8

I have list A with "Title" column which is lookup column to List B. I need to update existing List B documents so that they will be linked to List A. However, SharePoint client context doesn't accept extra argument in FieldLookupValue().

I thought I could code like:

ListItem i; // handle of List B item
var _newLookupField = new SP.FieldLookupValue();
_newLookupField.LookupId = 1;
_newLookupField.LookupValue = "Project A";
i["Project"] = _newLookupField; // Project is lookup column in List B
i.Update();

_clientcontext.ExecuteQuery();

But this doesn't work in Client model. Any help is must appreciated.

12

In order to set a lookup field using CSOM, the ID of the item from the lookup list should be specified. This ID can then be used to set the listitem's FieldLookupValue.

 FieldLookupValue lv = new FieldLookupValue();
 lv.LookupId = 1;
 item["Project"] = lv;
 item.Update();
 clientContext.ExecuteQuery();

Below is provided the complete example for specifying a lookup field using CSOM

public static void SetListItemLookupValue()
{
            ClientContext clientContext = new ClientContext("http://intranet.contoso.com/");
            List list = clientContext.Web.Lists.GetByTitle("Docs");
            FieldCollection fields = list.Fields;
            CamlQuery camlQueryForItem = new CamlQuery();
            camlQueryForItem.ViewXml = @"<View>
                                    <Query>
                                        <Where>
                                            <Eq>
                                                <FieldRef Name='ID'/>
                                                <Value Type='Counter'>4</Value>
                                            </Eq>
                                        </Where>
                                    </Query>
                                </View>";
            ListItemCollection listItems = list.GetItems(camlQueryForItem);
            clientContext.Load(listItems, items => items.Include
                                            (listItem => listItem["Project"],
                                             listItem => listItem["Editor"],
                                             listItem => listItem["Title"]));
            clientContext.ExecuteQuery();
            ListItem item = listItems[0];

            FieldLookupValue lv = item["Project"] as FieldLookupValue;
            if(lv == null) lv = new FieldLookupValue();
            lv.LookupId = 1;
            item["Project"] = lv;
            item.Update();
            clientContext.ExecuteQuery();  
}
  • 1
    Worked like a charm! Thank you :) – ZooZ May 14 at 11:36
3

Try this method. Here I am assuming that i is an item from the list.

var _newLookupField = new SP.FieldLookupValue();
var newId = 1;
_newLookupField.set_lookupId(newID);

i.set_item("Project", _newLookupField);
i.Update();
//Etc...
_clientcontext.ExecuteQueryAsync(/**callbacks**/);

EDIT: Ok, I just looked at your code and realized that might be C# but I automatically assumed it was JavaScript. Is this question MANAGED Client Object Model?

  • Yes Its C# managed client object model. – Rishi Jul 22 '13 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.