1

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.Choices.Add(txtNewChoice.Text);

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

            myList.Update();

            myWeb.Update();
            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

1
  • 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
    Commented Oct 11, 2012 at 9:02

2 Answers 2

4

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)
{
  //Declarations
  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;
  spFieldChoice.Update();

  //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: http://sharepoint.stackexchange.com/questions/29519/add-choice-to-spfieldchoice-from-an-eventreceiver-in-sandboxed
  //--------------

  //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>"); 
  }
  else
  { fieldSchema = fieldSchema.Replace("<CHOICES/>", "<CHOICES>" + choiceElements + "</CHOICES>"); 
  }

  spFieldChoice.SchemaXml = fieldSchema;

  return spFieldChoice;
}
2

the following post: http://mastervanou.wordpress.com/2009/05/14/return-values-of-an-spfieldchoice-object/ described how you should programmatically add choices.

1
  • 1
    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
    Commented Feb 16, 2012 at 12:30

Your Answer

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

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