SPFieldCollection.AddLookup Method (String, Guid, Guid, Boolean)

Creates a lookup field in the collection of fields for one list that points to a field in the collection for another list in a different website.

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

Syntax

'Declaration
Public Function AddLookup ( _
    displayName As String, _
    lookupListId As Guid, _
    lookupWebId As Guid, _
    bRequired As Boolean _
) As String
'Usage
Dim instance As SPFieldCollection
Dim displayName As String
Dim lookupListId As Guid
Dim lookupWebId As Guid
Dim bRequired As Boolean
Dim returnValue As String

returnValue = instance.AddLookup(displayName, _
    lookupListId, lookupWebId, bRequired)
public string AddLookup(
    string displayName,
    Guid lookupListId,
    Guid lookupWebId,
    bool bRequired
)

Parameters

  • displayName
    Type: System.String

    A string that specifies the display name of the field.

  • lookupListId
    Type: System.Guid

    A System.Guid object that specifies the target list for the lookup field.

  • lookupWebId
    Type: System.Guid

    A System.Guid object that specifies the target website for the lookup field.

  • bRequired
    Type: System.Boolean

    true if the field is required to contain values; otherwise, false.

Return Value

Type: System.String
A string that contains the internal name that is used for the field. You can retrieve the new field by passing this value to the GetFieldByInternalName(String) method. The field that is returned is of type SPFieldLookup.

Remarks

This method creates a field of type SPFieldLookup in the current list's field collection. A lookup field in one list takes its value from a field in another list, the target list specified in the lookupListId parameter. After you add a lookup field to the collection, you should retrieve it from the collection and then identify the source field in the target list by setting the LookupField property.

The target list that is the source of the lookup field's value is aware of the lookup field; that is, you can discover the lookup field by examining the objects in the collection returned by the target list's GetRelatedFields() method.

The current user must have SPBasePermissions.ManageLists permission on the target list when you call this method.

See Also

Reference

SPFieldCollection Class

SPFieldCollection Members

AddLookup Overload

Microsoft.SharePoint Namespace

SPFieldLookup

LookupField

GetFieldByInternalName(String)

AddDependentLookup(String, Guid)

GetRelatedFields()