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)

Public Property LookupField As String
Dim instance As SPFieldLookup
Dim value As String

value = instance.LookupField

instance.LookupField = value

Property value

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

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.

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.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using siteCollection As New SPSite("http://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
                End If
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
    End Sub
End Module