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.
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,
LikeLike
For Lookup fields, we need to pass the unique id of the text values. Any other format will be ignored. You can refer this as well, https://piyushksingh.com/2014/01/19/update-a-lookupfield-value-for-a-sharepoint-listitem-using-client-object-model-c/
LikeLike