SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am not able to add a new choice value to a SPFieldChoice column of a list. Here is my code

        using (SPWeb myWeb = SPContext.Current.Web)
            myWeb.AllowUnsafeUpdates = true;
            SPList myList = myWeb.Lists["DemoList"]; //Gets a list named "MyList"

            //Gets the field named "ChoiceField". Since ChoiceField is an SPFieldChoice I can cast it as
            // such. SPFieldChoice also inherits SPField.
            SPFieldChoice dropDown = (SPFieldChoice)myList.Fields["My Choice"];

            //Add some values

            dropDown.Update(); //Need to update the column for the changes to take affect


            myWeb.AllowUnsafeUpdates = false;

            foreach (var item in dropDown.Choices)
                result.Text += item.ToString();

Please let me know if I missed any point. NOTE I am using a sandboxed solution to deploy the web part

share|improve this question
Did you get the solution for this? I am also unable to add new choice value to a SPFieldChoice column. I was able to add the same using a Console Application. I assume that is this one of the limitation in Sandboxed Solution. Any suggestions from others :) – suryakiran Oct 11 '12 at 9:02

This thread might be a little stale but this could help someone else. Here is what I'm doing:

public static SPFieldChoice Create_Choice(SPFieldCollection spFieldCollection, string staticName, string displayName, string description, string[] choices, bool allowFillInChoices, bool required)
  SPFieldChoice spFieldChoice;
  string fieldSchema;
  string choiceElements = string.Empty;
  int choiceIndexOfStart;
  int choiceIndexOfEnd;

  //Create field.
  if (spFieldCollection.TryGetFieldByStaticName(staticName) == null)
  { spFieldCollection.Add(spFieldCollection.CreateNewField(SPFieldType.Choice.ToString(), staticName));        
  spFieldChoice = (SPFieldChoice)spFieldCollection.TryGetFieldByStaticName(staticName);
  spFieldChoice.Title = displayName;
  spFieldChoice.StaticName = staticName;
  spFieldChoice.Description = description;
  spFieldChoice.Required = required;
  spFieldChoice.FillInChoice = allowFillInChoices;

  //Add choices. 
  //Note: There is a bug in the SP object model when adding choices through a Sandbox solution. The logical way of
  //adding choices does not work. Below is a workaround which adds choices through the SchemaXml directly. The Update() method
  //is not called after making these changes.
  //See the following for more information:

  //Build choice xml element.
  foreach (string choice in choices)
  { choiceElements += "<CHOICE>" + choice + "</CHOICE>";

  //Update field schema.
  fieldSchema = spFieldChoice.SchemaXml;
  choiceIndexOfStart = fieldSchema.IndexOf("<CHOICES>");
  if (choiceIndexOfStart >= 0)
  { choiceIndexOfEnd = fieldSchema.IndexOf("</CHOICES>");
    fieldSchema = fieldSchema.Remove(choiceIndexOfStart, choiceIndexOfEnd - choiceIndexOfStart); 
    fieldSchema = fieldSchema.Replace("</CHOICES>", "<CHOICES>" + choiceElements + "</CHOICES>"); 
  { fieldSchema = fieldSchema.Replace("<CHOICES/>", "<CHOICES>" + choiceElements + "</CHOICES>"); 

  spFieldChoice.SchemaXml = fieldSchema;

  return spFieldChoice;
share|improve this answer

the following post: described how you should programmatically add choices.

share|improve this answer
The method described in the link uses elevated privileges, which are not allowed in sandboxed solutions. Are elevated privileges necessary for adding choices? – Janne Louw Feb 16 '12 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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