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();
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();
  • 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. – Mayank Srivastava Feb 13 '17 at 13:27
  • Please check edited answer. it's working or not – Samir Khimani Feb 13 '17 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.

  • Hi Jinesh, I have to do it through client object model not javascript object model. I'm using C#. – Mayank Srivastava Feb 13 '17 at 13:33
  • please see edited answer. for more info visit this link – Jinesh Kaneriya Feb 13 '17 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();

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.