Karine Bosch’s Blog

On SharePoint

Setting the value of a lookup field using CSOM

Today I had a hard time to find out how to set the value of a lookup field using CSOM. In most of the code samples I found on the internet, the LookupId is set hard coded, and this is not what I needed. Additionally, in some cases the lookup list was situated on the root web, and in another case I had to query a different field than the Title field. I ended up writing the following method:

public static FieldLookupValue GetLookupValue(ClientContext clientContext, string value, 
   string lookupListName, string lookupFieldName, string lookupFieldType, bool onRootWeb)
     List list = null;
     FieldLookupValue lookupValue = null;

     if (onRootWeb)
          list = clientContext.Site.RootWeb.Lists.GetByTitle(listName);
          list = clientContext.Web.Lists.GetByTitle(listName);

     if (list != null)
         CamlQuery camlQueryForItem = new CamlQuery();
         camlQueryForItem.ViewXml = string.Format(@"<View>
                             <FieldRef Name='{0}'/>
                             <Value Type='{1}'>{2}</Value>
            </View>", lookupFieldName, lookupFieldType, value);

          listItemCollection listItems = list.GetItems(camlQueryForItem);
          clientContext.Load(listItems, items => items.Include
                                            (listItem => listItem["ID"],
                                             listItem => listItem[lookupFieldName]));

          if (listItems != null)
              ListItem item = listItems[0];
              lookupValue = new FieldLookupValue();
              lookupValue.LookupId = Int.Parse(item["ID"].ToString());

     return lookupValue;

This method works on both SharePoint 2010 and 2013.

I hope this code snippet can help others.




May 11, 2015 - Posted by | CSOM, SharePoint 2010


  1. […] Setting the value of a lookup field using CSOM […]

    Pingback by SharePoint 2013 & Office 365: Recopilatorio de enlaces interesantes (LXVII)! - Blog de Juan Carlos González en Geeks.MS | June 1, 2015 | Reply

  2. […] (Most of this code is sourced from Karine Bosch’s blog post) […]

    Pingback by Using CSOM to Import CSV Files Into Lists in Office 365/SharePoint Online - www.credera.com | April 13, 2016 | Reply

  3. lambada expression is not working ,hass error ,”could not convert lambada expression to type ‘syste,.linq.expressions.expressin<syste,.func[].becasue’ it is not a delegate type”.

    Comment by hari | December 27, 2017 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: