In this hint, I will explain How to Get a SharePoint Choice Field Values into a checkbox list and dropdown list using C# Server Object Model?
You may also like to read Bind dropdown from SharePoint List in C#.
Consider, you have a SharePoint list “Country”, you need to retrieve a specific Choice Field value to a Dropdown list in C#.
Steps
- Open your SharePoint Solution.
- Add the following function in your code.
public void BindCountry() {
using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
using(SPWeb web = site.OpenWeb()) {
SPList list = web.Lists["Country"];
SPFieldChoice myChoicesfield = (SPFieldChoice) list.Fields["FieldName"];
for (int i = 0; i < myChoicesfield.Choices.Count; i++) {
DropDownListName.Items.Add(myChoicesfield.Choices[i].ToString());
}
}
}
}
- Call it in Page_Load with no postback.
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
BindCountry();
}
public void BindCountry() {
using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
using(SPWeb web = site.OpenWeb()) {
SPList list = web.Lists["Country"];
SPFieldMultiChoice choices = (SPFieldMultiChoice) list.Fields[FieldName];
foreach(string str in choices.Choices) {
cblCheckboxList.Items.Add(new ListItem(str, str));
}
}
}
}
Conclusion
In conclusion, we have learned how to
- Retrieve SharePoint Choice Field Value to a dropdown list in C#.
- Retrieve SharePoint Choice Field Value to a checkbox list in C#.
Applies To
- SharePoint 2016.
- SharePoint 2013 / 2010.
- C# SSOM.
You might also like to read
- Get and Set a SharePoint Multiple Lookup Field Value Using Server Object Model C#.
- SharePoint Lookup Field Multiple Values Via C#.
- How to get Title and URL of SharePoint Page via Object Model?
- Get and Set a SharePoint Lookup Field Value Using Server Object Model C#.
- Get and Set a SharePoint URL Field Value Using Server Object Model C#.
- Get and Set a SharePoint Multiple Choice Field Value Using Server Object Model C#.
- Get and Set a SharePoint Multiple Lookup Field Value Using Server Object Model C#.
- Retrieve a SharePoint Choice Field Value Using Server Object Model C#.
Have a Question?
If you have any related questions, please don’t hesitate to Ask it at deBUG.to Community.
Pingback: Get and Set SharePoint User Field Value in C# | SPGeeks
Pingback: How to get Title and URL of SharePoint Page via Object Model | SPGeeks
Thanks for sharing