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)

public string AddLookup(
	string displayName,
	Guid lookupListId,
	Guid lookupWebId,
	bool bRequired


Type: System.String

A string that specifies the display name of the field.

Type: System.Guid

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

Type: System.Guid

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

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.

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.