0

I am trying to update a multi value lookup column in SP 2013 On Premise. I have gone through various forums for achieving this and as suggested, I am getting values in SPFieldLookupValueCollection. Sample values in this are of the format:

4;#N/A

The problem I am facing is if I try to update the column using :

item["ColumnName"] = SPfieldLookupvaluecollection object

I am getting an exception saying that item is being updated in the wrong format. the closest I've come to updating the lookup column is by using a foreach loop and iterating the lookupvaluecollection. But this doesn't help me since I have multiple values in the SPfieldLookupvaluecollection object and only the last value is getting updated. Any help would be appreciated.

Thanks

Edit : Updating with the code snippet.

Microsoft.SharePoint.Client.File newfile = myclientcontext.Web.GetFileByServerRelativeUrl("Relative path to file here"); myclientcontext.load(newfile); ListItem item = newfile.ListItemAllFields;

//I'm reading lookup values from a csv file and updating accordingly

if (rowfilter.ItemArray[0].ToString() != "") { string[] values = rowfilter.ItemArray[0].ToString().Split(';');

SPFieldLookupValueCollection fieldlookup = GetLookupFieldIDs(values, "LookupListName"); }

//Above method returns values in the format 4;#N/A 106;#Building //This is what i've tried

  1. item["ColumnName"] = fieldlookup;
  2. var arraylist = new ArrayList(); foreach (SPFieldLookupValue value in fieldlookup) { arraylist.Add(value.LookupId); } item["ColumnName"] = arraylist.ToArray();

However I get an error always. Any help would be appreciated.

  • Try to change the field value in your lookup list to NA instead of N/A, that causes the value in a wrong format. if it does not help edit your question with the full code snippet – Mohamed El-Qassas MVP Sep 22 '18 at 0:46
  • Thanks for replying Mohamed, unfortunately changing the value from N/A to NA is not an option. Also, I am able to update the value to N/A as required. The problem I am facing is in updating multiple values to the List item. I am adding the code snippet above. – Hank Sep 24 '18 at 11:00
0

We can use the following CSOM code snippet to update multiple lookup columns.

var siteUrl = "http://sp2013";
var username = "username";
var password = "password";
var domain = "domain";
var listName = "customlist";
var itemId = 1;
var lookupColumnName = "MultipleLookup";
using (ClientContext ctx = new ClientContext(siteUrl))
{
    ctx.Credentials = new NetworkCredential(username, password, domain);
    var list = ctx.Web.Lists.GetByTitle(listName);
    var item = list.GetItemById(itemId);               
    ctx.Load(item, i => i[lookupColumnName]);
    ctx.ExecuteQuery();
    var lookupValues = new ArrayList();            
    var lookupValue = new FieldLookupValue { LookupId = 5 };
    lookupValues.Add(lookupValue);
    var lookupValue1 = new FieldLookupValue { LookupId = 2 };
    lookupValues.Add(lookupValue1);
    item[lookupColumnName] = lookupValues.ToArray();
    item.Update();
    ctx.ExecuteQuery();
}

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.