Sharepoint - SharePoint client model and setting up lookup field

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();  
}

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?

Tags:

.Net