Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
881 views
in SharePoint Server by 66 69 85

I want to get the options from the select field in a SharePoint list so I can put them to a drop-down list in my web part.

Below is what I tried, but it didn't work:

SPList list = web.Lists["myList"];
SPFieldChoice dropdown = list.Fields["FieldName"] as SPFieldChoice;
myDDL = dropdown;

Different options were also tried, but nothing was able to get the choices from the select field.


1 Answer

1 like 0 dislike
by 66 69 85
 
Best answer

How to get select option from SharePoint list in C#?

The only way that I could grab the options from the select field in SharePoint list was the the following code:

# get choices from select column
var cities= (list.Fields.GetField("Cities") as SPFieldChoice).Choices;

# add the choices to dropDown list
foreach(var city in cities){
    ddlCity.Items.Add(city);
}
If you don’t ask, the answer is always NO!
...