1

I have a requirement where I have to select the values of a choice field while creating a new item using CSOM. I'm able to update other fields, but for choice column, the value gets stored in the Specify your own value column, although the values are present in that particular choice column.

enter image description here

I assume my syntax for updating choice column is not correct. Here is the code:

ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle("testList");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);

oListItem["Name"] = "Sherlock Holmes";
oListItem["Phone"] = "0000000000";
oListItem["Address"] = "221B Baker Street";
oListItem["ChoiceOfCuisine"] = "Cajun,Persian cuisine,Zanzibari";
oListItem.Update();
clientContext.ExecuteQuery();

4 Answers 4

3

Can you please try below code.

ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle("testList");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
List<string> Items = new List<string>();
Items.Add("Cajun");
Items.Add("Persian cuisine");
Items.Add("Zanzibari");
oListItem["Name"] = "Sherlock Holmes";
oListItem["Phone"] = "0000000000";
oListItem["Address"] = "221B Baker Street";
oListItem["ChoiceOfCuisine"] = Items;
oListItem.Update();
clientContext.ExecuteQuery();
2
  • I think this would add the options to my list. The options are already there. I just want the given options to be saved to the list. Feb 13, 2017 at 13:27
  • Please check edited answer. it's working or not Feb 13, 2017 at 13:44
2

you have to pass array like

            object[] fv = oListItem["ChoiceOfCuisine"] as object[];
            List<object> newChoiceOfCuisine = fv.ToList<object>();
            newChoiceOfCuisine.Add("Cajun");
            newChoiceOfCuisine.Add("Persian");
            newChoiceOfCuisine.Add("XYZ");
            oListItem["ChoiceOfCuisine"] = newLanguages.ToArray<object>();

hope this will work.

2
  • Hi Jinesh, I have to do it through client object model not javascript object model. I'm using C#. Feb 13, 2017 at 13:33
  • please see edited answer. for more info visit this link Feb 13, 2017 at 13:56
2

You can try it as below:

ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle("testList");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);

oListItem["Name"] = "Sherlock Holmes";
oListItem["Phone"] = "0000000000";
oListItem["Address"] = "221B Baker Street";
oListItem["ChoiceOfCuisine"] = new string[] {"Cajun,Persian cuisine,Zanzibari"};
oListItem.Update();
clientContext.ExecuteQuery();
-1

update list item dynamically, tried and tested, For SharePoint Multi line choice column

List list = new List(); list .Add(";#" + strChoice);
list .Add(";#" + strChoice2);
string[] strSelectChoices = list.ToArray();
oListItem["ColumnName"] = strSelectChoices ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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