Search This Blog

Thursday 22 March 2012

Get values from SharePoint 2010 lookup field in a listbox

This is an example for adding values from a SharePoint 2010 lookup filed into a asp.net listbox control using SharePoint 2010 client Object Model.


ClientContext clientContext = new ClientContext(“https://mySPsite”);
List list = clientContext.Web.Lists.GetByTitle(“MYList”);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = “<View/>”;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (ListItem listItem in listItems)
{
// Get value
SP.FieldLookupValue _value = listItem.FieldValues["MyLookupFieldID"] as SP.FieldLookupValue;
var mylookupvalue= _value.LookupValue;
listBoxControl1.Items.Add(mylookupvalue);
}

No comments:

Post a Comment