SharePoint Get Choice Column Values C#

Get and Set SharePoint Field using C#

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#.


SharePoint get choice field value programmatically

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();
}

Retrieve SharePoint Choice Field Value to a checkbox list in C#

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
Have a Question?

If you have any related questions, please don’t hesitate to Ask it at deBUG.to Community.

3 thoughts on “SharePoint Get Choice Column Values C#”

  1. Pingback: Get and Set SharePoint User Field Value in C# | SPGeeks

  2. Pingback: How to get Title and URL of SharePoint Page via Object Model | SPGeeks

Leave a Reply