Get and Set a SharePoint Multiple Lookup Field Value Programmatically

Get and Set SharePoint Field using C#

In this post, I will explain how to Get and Set a SharePoint Multiple Lookup Field Value programmatically using C#?


How to Get and Set a SharePoint Multiple Lookup Field Value Programmatically using C#?

In this section, we will show how to get and set SharePoint Multiple Lookup Field value in SharePoint List using C# (Server Side Object Model).

Get and Set a SharePoint Multiple Lookup Field Value Programmatically

Set SharePoint Multiple Lookup Field Value

Consider, you have a SharePoint List with a Lookup (Allow Multiple Values) field and you would like to update and set the SharePoint Multiple Lookup Field Value programmatically using C#.

In this case, you should use the below “setMultipleLookupFieldValue()” function with your List name and field name to can set Lookup (Allow Multiple Values) field Value using C#.

C# Code

public void setMultipleLookupFieldValue() {
 using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
  using(SPWeb web = site.OpenWeb()) {
   SPList list = web.Lists["Users"];
   SPListItem item = list.GetItemById(1);
   SPFieldLookupValueCollection itemValues = SPFieldLookupValueCollection();
   itemValues.Add(new SPFieldLookupValue(1, "Title"));
   item["FieldName"] = itemValues;
   item.Update();
  }
 }
}

Download the full code for SharePoint Lookup Field Operations Solution in C#

Get SharePoint Multiple Lookup Field Value

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

C# Code

public void setMultipleLookupFieldValue() {
 using(SPSite site = new SPSite(SPContext.Current.Site.Url)) {
  using(SPWeb web = site.OpenWeb()) {
   SPList list = web.Lists["Users"];
   SPListItem item = list.GetItemById(1);
   SPFieldLookupValueCollection itemValues = item["FieldName"] 
as SPFieldLookupValueCollection;
     foreach (SPFieldLookupValue itemValue in itemValues)
      {
        int id = itemValue.LookupId;
        string value = itemValue.LookupValue;
      }
  }
 }
}

It’s strongly recommended to read Work with SharePoint Lookup Field Programmatically


Conclusion

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

Download
Applies To
  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint 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.

5 thoughts on “Get and Set a SharePoint Multiple Lookup Field Value Programmatically”

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

  2. Pingback: Bind dropdown from SharePoint List in C# | SPGeeks

  3. Pingback: Get and Set SharePoint Multiple Choice Field Value in C# | SPGeeks

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

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

Leave a Comment

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

Scroll to Top