SPFieldLookupValue Class

Contains the value for an SPFieldLookup object.


Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

[ClientCallableTypeAttribute(Name = "FieldLookupValue", ServerTypeId = "f1d34cc0-9b50-4a78-be78-d5facfcccfb7", 
	ValueObject = true)]
public class SPFieldLookupValue

Instances of this class represent a single field value for a lookup field. Lookup fields can have multiple values or just one. You can determine which is the case by checking the value of the lookup field's AllowMultipleValues property. If the property returns true, the field can have multiple values; if it returns false, it cannot.

If a lookup field does not allow multiple values, the field value is an object of type String that contains the ID of the item in the list that the lookup field points to plus the value of the field in the item that the lookup field points to. You can pass this string as an argument to the SPFieldLookupValue(String) constructor to create an SPFieldLookupValue object. Then you can get the list item ID from the LookupId property and the value of the field in the list item from the LookupValue property.

If a lookup field allows multiple values, then the field value is an object of type SPFieldLookupValueCollection that is boxed as type Object. The SPFieldLookupValueCollection object is a collection of SPFieldLookupValue objects. You can extract the field values by enumerating the collection and accessing each object's LookupValue property.

The following example demonstrates how to set the value of a lookup field.

The example code is a console application that gets references to two related lists, Customers and Orders. Each item in the Customers list represents a customer record, and the ID field in the item is used to identify a customer. Each item in the Orders list represents an order placed by a customer. The Orders list has a lookup field named Customer ID that points to the ID field in the Customers list, identifying the customer who placed the order.

Code in a foreach loop iterates through the list of customers, adding a new item to the Orders list for each customer on the Customers list. In each case, the code sets the value of the lookup field, Customer ID, to link the order to the customer.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
    class Program
        static void Main(string[] args)
            using (SPSite site = new SPSite("http://localhost"))
                using (SPWeb web = site.RootWeb)
                    SPList customerList = web.Lists.TryGetList("Contoso Customers");
                    SPList orderList = web.Lists.TryGetList("Contoso Orders");

                    if (customerList != null && orderList != null)
                        SPListItemCollection customers = customerList.Items;
                        SPListItemCollection orders = orderList.Items;

                        string fieldName = "CustIDLookup";
                        if (!orderList.Fields.ContainsField(fieldName))
                        SPField lookupFld = orderList.Fields.GetField(fieldName);

                        foreach (SPListItem customer in customers)
                            SPListItem order = orders.Add();
                            order[SPBuiltInFieldId.Title] = "Thank you!";

                            SPFieldLookupValue value = new SPFieldLookupValue(customer.ID, customer.ID.ToString());
                            order[lookupFld.Id] = value.ToString();

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.