I was recently working on some code that added items to a SharePoint list based on a user’s input from an InfoPath form. It was an easy task until I got to a choice column. I wasn’t sure of the syntax for setting the value of a choice column, and for some reason, all of the blog posts I found focused on adding values to a choice column. After some trial and error, I figured out this solution.
using (SPSite site = new SPSite("http://mysite.local/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My List"];
string myChoice = “My Choice”;
SPListItem item = list.Items.Add();
item["Choice Column"] = list.Fields["Choice Column"].GetFieldValue(myChoice);
item.Update();
}
}
As long as the value of the parameter passed into GetFieldValue() matches an existing value in the choice column, a new list item will be added with the specified value in the choice column.