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

'Declaration
Public Property LookupField As String
    Get
    Set
'Usage
Dim instance As SPFieldLookup
Dim value As String

value = instance.LookupField

instance.LookupField = value
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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using siteCollection As New SPSite("https://localhost")
            Using site As SPWeb = siteCollection.OpenWeb()
                Dim lookupList As SPList = site.Lists.TryGetList("Customers")
                Dim relatedList As SPList = site.Lists.TryGetList("Pending Orders")

                If lookupList IsNot Nothing AndAlso relatedList IsNot Nothing Then
                    Dim strPrimaryCol As String = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, True)
                    Dim primaryCol As SPFieldLookup = _
                        DirectCast(relatedList.Fields.GetFieldByInternalName(strPrimaryCol), SPFieldLookup)

                    primaryCol.LookupField = lookupList.Fields("ID").InternalName
                    primaryCol.Indexed = True
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict
                    primaryCol.Update()
                End If
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module

See also

Reference

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace

FieldId

LookupList

LookupWebId