Reputation: 21
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
Upvotes: 2
Views: 18562
Reputation: 114
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]);
}
Upvotes: 6
Reputation: 9739
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);
}
Upvotes: 3
Reputation: 1313
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
Upvotes: 1