3

I am relatively new to sharepoint and I'm trying to write a web service to return our sharepoint inventory data as xml. It works good except that one of those list includes a lookup field and the generated xml contains "Microsoft.SharePoint.Client.FieldLookupValue" instead of the expected string value of the lookup field.

This is the code I'm using to generate the xml:

resultList = remoteWeb.Lists.GetByTitle("Cam Devices");
context.Load(resultList);
context.ExecuteQuery();
//Now its time to reach list's items
items = resultList.GetItems(new CamlQuery());
context.Load(items);
context.ExecuteQuery();
foreach (ListItem item in items)
{
    rootNode.AppendChild(doc.CreateElement("ID")).InnerText = "pcat:401824";
    rootNode.AppendChild(doc.CreateElement("Category")).InnerText = "Cam Devices";
    rootNode.AppendChild(doc.CreateElement("Kimlik")).InnerText = Convert.ToString(item["ID"]);
    rootNode.AppendChild(doc.CreateElement("Isim")).InnerText = Convert.ToString(item["Location0"]) + " >> " + Convert.ToString(item["Brand"]) + " >> " + Convert.ToString(item["ID"]);
}

item["Location"] is the lookup field and it has a value with the type FieldLookupValue, how can I get the lookup value as a string?

Alex
  • 14,104
  • 11
  • 54
  • 77
Mehmet Noyan Aydin
  • 289
  • 1
  • 4
  • 15
  • This also Helpful [Get and Set a SharePoint Lookup Field Values Using SSOM C#](https://social.technet.microsoft.com/wiki/contents/articles/40271.get-and-set-a-sharepoint-lookup-field-values-using-ssom-c.aspx) – Mohamed Oct 22 '17 at 09:36

1 Answers1

16

Ok, successfully get the lookup field's value by using following code syntax:

string Location = "";
if (item["Location0"] != null)
{
    var fl = (SPFieldLookupValue)item["Location0"];
    Location = fl.LookupValue;
}
Alex
  • 14,104
  • 11
  • 54
  • 77
Mehmet Noyan Aydin
  • 289
  • 1
  • 4
  • 15