Get and Set a SharePoint Multiple Choice Field Value in C#

Get and Set SharePoint Field using C#

In this hint, I will explain How to Get and Set a SharePoint Multiple Choice Field Value programmatically using C# SSOM Server Object Model?


Get and Set a SharePoint Multiple Choice Field Value in C#

Set SharePoint Choice Field Multiple Selection Value Using C#

Consider, you have a SharePoint List with a Choice Multiple Selection field and you would like to update and set this multiple choice Field Value programmatically using C#.

Get and Set a SharePoint Multiple Choice Field Value in C#

In this case, you should use the below “SetMultipleChoiceFieldValue()” function with your List name and field name to be able to set Multiple choice field value using C#.

C# Code

public void SetMultipleChoiceFieldValue() {
 using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
  using(SPWeb web = site.OpenWeb()) {
   SPList list = web.Lists["List1"];
   SPListItem item = list.GetItemById(1);
   SPFieldMultiChoiceValue itemValue = new SPFieldMultiChoiceValue();
   itemValue.Add("Choice 1");
   itemValue.Add("Choice 2");
   itemValue.Add("Choice 3");
   item["FieldName"] = itemValue;
   item.Update();
  }
 }
}

Note: the Field Class is SPFieldMultiChoice, and the Field Value Class is SPFieldMultiChoiceValue


Get SharePoint Choice Field Multiple Selection Value Using C#

In the previous section, we have learned how to set Choice Field Multiple Selection Value programmatically using C#, In this section, we will show how to get SharePoint Choice Field Multiple Selection Value Using C#.

C# Code

public void SetMultipleChoiceFieldValue() {
 using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
  using(SPWeb web = site.OpenWeb()) {
   SPList list = web.Lists["List1"];
   SPListItem item = list.GetItemById(1);
   SPFieldMultiChoiceValue itemValue = 
new SPFieldMultiChoiceValue(item["FieldName"].ToString());
     foreach (string choice in itemValue)
      {
        // value is in choice
      }
  }
 }
}

Conclusion

In conclusion, we have learned how to Get and Set a SharePoint Multiple Choice Field Value using Server Object Model C#?

Applies To
  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint 2010.
  • C# SSOM.
You may also like to read
Have a Question?

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

2 thoughts on “Get and Set a SharePoint Multiple Choice Field Value in C#”

  1. Pingback: Get SharePoint Choice Field Value In C# | SPGeeks

  2. Pingback: Get URL value from Hyperlink Field in SharePoint using C# | SPGeeks

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top