Share via


SPFieldLookup.LookupField property

Gets or sets the internal name of the field in a related list that is the source of this field's value.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

public string LookupField { get; set; }

Property value

Type: System.String
A string that contains the internal name of the field.

Remarks

After you add a lookup field to a list's collection by calling the AddLookup method, you should retrieve the lookup field from the collection and identify the source field in the target list by setting the LookupField property. When you set the LookupField property you must call the Update method for changes to take effect in the database.

The target of the LookupField property must be one of the following field types:

  • SPFieldType.Counter

  • SPFieldType.DateTime

  • SPFieldType.Number

  • SPFieldType.Text

In addition, SPFieldType.Computed is allowed as a target if lookups are enabled. For more information, see the EnableLookup property of the SPFieldComputed class. The SPFieldType.Calculated field type can be a target if the output is text. For more information, see the OutputType property of the SPFieldCalculated class.

Examples

The following example is a console application that creates a relationship between the Customers list and the Pending Orders list. The application calls the AddLookup method to add a lookup field named Customer ID to the Pending Orders list and points the field at the ID field on the Customers list. The new Customer ID field is indexed and set to restrict deletions from the lookup list.

using System;
using Microsoft.SharePoint;

namespace RelatedLists
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList lookupList = site.Lists.TryGetList("Customers");
                    SPList relatedList = site.Lists.TryGetList("Pending Orders");

                    if (lookupList != null && relatedList != null)
                    {
                        string strPrimaryCol = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, true);
                        SPFieldLookup primaryCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPrimaryCol);

                        primaryCol.LookupField = lookupList.Fields["ID"].InternalName;
                        primaryCol.Indexed = true;
                        primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                        primaryCol.Update();
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

See also

Reference

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace

FieldId

LookupList

LookupWebId