Update a LookupMulti Column value in SharePoint using Client Object Model C#

Update the value of LookupMulti column in SharePoint using the Client Object Model
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
ListItem currentItem = list.GetItemById(1);

FieldLookupValue[] lookupFieldValCollection = new FieldLookupValue[3];FieldLookupValue lookupFieldA = new FieldLookupValue();
lookupFieldA.LookupId = 2;
lookupFieldValCollection.SetValue(lookupFieldA, 0);
FieldLookupValue lookupFieldB = new FieldLookupValue();
lookupFieldB.LookupId = 4;
lookupFieldValCollection.SetValue(lookupFieldB, 1);
FieldLookupValue lookupFieldC = new FieldLookupValue();
lookupFieldC.LookupId = 6;
lookupFieldValCollection.SetValue(lookupFieldC, 2);

currentItem["MultiLookupValCol"] = lookupFieldValCollection;
currentItem.Update();

ctx.ExecuteQuery();  

Two important thing I would like to share here,

  • Initially I tried to pass an array of an array of integer [containing the ids of the item] to the LookupMulti column which generated the error: Value does not fall between the specified range
  • Another thing was then I created a List of type FieldLookupValue and then I passed the List to the column using its ToArray(). I guess we just had to pass the array of FieldLookupValue. However, this resulted in the error: Unknown Error. Don’t know what that means.

Finally, I can say that create the array of type FieldLookupValue and populate this array using its SetValue method only.

2 thoughts on “Update a LookupMulti Column value in SharePoint using Client Object Model C#

  1. Hello Piyush,

    I think this code is close to what I’m trying to do, which is using C# to add/update a legacy Sharepoint 2016 list. The list has some columns that have been set up as Lookup columns. The lookup is another Sharepoint list. When I try to update the list with text values that match the lookup list exactly, the item still doesn’t accept the value.

    The code I’m using looks like this –
    SP.ClientContext clientContext = new SP.ClientContext(siteUrl);
    clientContext.Credentials = new NetworkCredential(strUser, strPW, strDomain);
    SP.List oList = clientContext.Web.Lists.GetByTitle(“Project List”);
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    SP.ListItem oListItem = oList.AddItem(itemCreateInfo);
    oListItem[“Title”] = strTitle;
    oListItem[“Audit_x0020_Type”] = strAuditType;
    oListItem[“Audit_x0020_Fiscal_x0020_Year”] = DdlAuditFY.SelectedValue;

    It’s that final line, the ‘Audit Fiscal Year field, which will not accept the value. The code doesn’t throw any errors, it just leaves the column blank.
    Can you provide any insights?
    Thanks,

    Like

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.