Retrieve value of a Sharepoint Choice Field in c# Visual Studio 2010

18,008

Solution 1

See this blog post. This is the proper way of doing this. http://www.c-sharpcorner.com/Blogs/10257/

SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue(item["MultiChoice"].ToString());
for (int i = 0; i < choices.Count; i++)
{
    Console.WriteLine(choices[i]);
}

Solution 2

If you have a Choice column, where multiple Items can be chosen, you can use this to separate them:

string values = item["yourColumn"] as string;
string[] choices = null;
if (values != null)
{
    choices = values.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
}

Solution 3

I'm unsure as what you want to do. If you want to get all of the values from a field (choice) from a list i could suggest you get the list in to a object(SPList), iterate through the items (yourSPListObject.items)

// get the current web you are in
SPWeb objWeb = SPContext.Current.Site.OpenWeb();
//get your list
SPList lstYourInfoList = objWeb.Lists["<ListNameHere"];

//Iterate through the items in the list
foreach(SPListItem item in lstYourInfoList.items){
//pick out your information needed
string choiceSelected = item["<ColumnNamethatrepresentsyourchoicefield>"].ToString();
//store your information somewhere
//store the string in a local list and pass this list back out
}

This may help if you want to get all the choices that the user can select from

http://www.mindfiresolutions.com/SharePoint-Choice-Field--Fetch-Each-Choice-Item-80.php

hope this answers your Q

Share:
18,008
EST
Author by

EST

Updated on June 13, 2022

Comments

  • EST
    EST almost 2 years

    On an EventReceiver, using c# I want to retrieve all the selected values in a Sharepoint 2010 Choice field on a List. Can anyone advise/provide a code snippet on how to read all the values from a Choice field?

    Thanks