Share via


SharePoint: Get and Set Lookup Field Values Using SSOM C#

Introduction

In SharePoint List, you can create a column as a Lookup datatype that holds items from another list on the same site. 

https://gallery.technet.microsoft.com/site/view/file/180204/1/Get%20and%20Set%20Lookup%20column%20in%20SharePoint.png

The Lookup columns (SPFieldLookup) can have multiple values or just single value based on the AllowMultipleValues property. 

 

During creating a new Lookup column, If the AllowMultipleValues is checked, the column is multiple values; else, it's a single value.

https://gallery.technet.microsoft.com/site/view/file/180205/1/Allow%20Multiple%20values%20in%20Lookup%20field.png

In case, the lookup column is a single value, the field value is an object of type SPFieldLookupValue with

  • A 32-bit integer ID (LookupId) of the item in the list that the lookup field points to. 
  • A string value (LookupValue) of the field in the item that the lookup field points to. 

In case, the lookup column allows multiple values, then the field value is an object of type SPFieldLookupValueCollection that is a collection of SPFieldLookupValue object.

You might also like to read Using Lookup Field in Calculated Column SharePoint 

In this article, we will explain How to use Server Side Object Model (C#) to do the following:

  • Set a SharePoint Lookup Field (Single Value).
  • Set a SharePoint Lookup Field (Multiple Values).
  • Get a SharePoint SharePoint Lookup Field (Single Value).
  • Get a SharePoint SharePoint Lookup Field (Multiple Values).

https://gallery.technet.microsoft.com/site/view/file/180046/1/Get%20and%20Set%20Lookup%20Field%20Single%20%26%20Multiple%20values%20Uisng%20SSOM%20C%23.gif

Set SPLookup Field (Single Value) Using SSOM C#

The below code shows How to set a SPLookup field (Single Value) in a SharePoint List new item.

using (SPSite site = SPContext.Current.Site)
 {
 using (SPWeb web = site.OpenWeb())
                 {
 // Allow Unsafe Updates to prevent the cross site scripting
 web.AllowUnsafeUpdates = true;
 // Get The SPList
 SPList list = web.Lists["List Name"];
 // Add a new list item
 SPListItem item = list.Items.Add();
 // Set the default Title field
 item["Title"] = "String Value";
 //Set the Lookup Field - Single Value
 item["The Lookup Field Name"] = new  SPFieldLookupValue("A 32-bit integer that specifies the ID of the lookup field", "A string that contains the value of the lookup field");
 // Submit your Item
 item.Update();
 web.AllowUnsafeUpdates = false;
                 }
  
 }

Set SPLookup Field (Multiple Values) Using SSOM C#

The below code shows How to set a SPLookup field (Multiple Values) in a SharePoint List new item.

   using (SPSite site = SPContext.Current.Site)
 {
 using (SPWeb web = site.OpenWeb())
               {
 // Allow Unsafe Updates to prevent the cross site scripting
 web.AllowUnsafeUpdates = true;
 // Get The SPList
 SPList list = web.Lists["List Name"];
 // Add a new list item
 SPListItem item = list.Items.Add();
 // Set the default Title field
 item["Title"] = "string value";
 //Set Lookup Field - Multiple Values
 SPFieldLookupValueCollection itemValues = new  SPFieldLookupValueCollection();
 foreach (ListItem litem in your collection)
 {
 itemValues.Add(new SPFieldLookupValue("A 32-bit integer that specifies the ID of the lookup field", "A string that contains the value of the lookup field"));
 }
 item["The Lookup Field Name"] = itemValues;
  
 // Submit your Item
 item.Update();
 web.AllowUnsafeUpdates = false;
               }  
 }

Get SPLookup Field (Single Value) Using SSOM C#

The below code show How to get a SPLookup field (Single Values) based on an existing item in a SharePoint List.

   using (SPSite site = SPContext.Current.Site)
 {
 using (SPWeb web = site.OpenWeb())
               {
 // Get The SPList
 SPList list = web.Lists["List Name"];
 // Get a list item By ItemID
 SPListItem item = list.GetItemById("A 32-bit integer that specifies the ID of List Item");
 //Get Lookup Field - Single Value
 SPFieldLookupValue SingleValue = new  SPFieldLookupValue(item["The Lookup Field Name"].ToString());
 int SPLookupID = SingleValue.LookupId;
 string SPLookupValue = SingleValue.LookupValue;
               }  
 }

Get SPLookup Field (Multiple Values) Using SSOM C#

The below code shows How to get a SPLookup field (Multiple Values) in a dropdown list based on an existing item in a SharePoint List.

   using (SPSite site = SPContext.Current.Site)
 {
 using (SPWeb web = site.OpenWeb())
                 {
 // Get The SPList
 SPList list = web.Lists["LookupField"];
 // Get a list item By ItemID
 SPListItem item = list.GetItemById("A 32-bit integer that specifies the ID of List Item");
 //Get Lookup Field - Multiple Values
 SPFieldLookupValueCollection MultipleValues = item["The Lookup Field Name"] as  SPFieldLookupValueCollection;
 foreach (SPFieldLookupValue itemValue in MultipleValues)
 {
 ListItem litem = new  ListItem();
 litem.Text = itemValue.LookupValue;
 litem.Value = itemValue.LookupId.ToString();
 dropdownlist.Items.Add(litem);
 }
                 }  
 }

Download

You can download the full solution from

Reference

This article credit to me at Work with SharePoint Lookup Field Programmatically

Conclusion

In this article, we have explained How to use Server Side Object Model - C# to do the following:

  • Set a SharePoint Lookup Field (Single Value).
  • Set a SharePoint Lookup Field (Multiple Values).
  • Get a SharePoint SharePoint Lookup Field (Single Value).
  • Get a SharePoint SharePoint Lookup Field (Multiple Values).

See Also

Back To Top