Export (0) Print
Expand All

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

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.

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.

Show:
© 2014 Microsoft