SPFieldCollection.AddLookup method (String, 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 the same website.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

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


Type: System.String

A string that specifies the display name of the field.

Type: System.Guid

A GUID that specifies the target list for the lookup field.

Type: System.Boolean

true to require that the field 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.

The following example is a console application that gets the collection of fields associated with the Pending Orders list and adds a lookup field named Customer ID that points to the ID field in the Customers list. The code then creates a secondary field that depends on the Customer ID field for its relationship to the Customers list.

using System;
using Microsoft.SharePoint;

namespace RelatedLists
    class Program
        static void Main(string[] args)
            using (SPSite siteCollection = new SPSite("http://localhost"))
                using (SPWeb site = siteCollection.OpenWeb())
                    SPList sourceList = site.Lists["Customers"];
                    SPList dependentList = site.Lists["Pending Orders"];

                    string strPrimaryCol = dependentList.Fields.AddLookup("Customer ID", sourceList.ID, true);
                    SPFieldLookup primaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strPrimaryCol);
                    primaryCol.LookupField = sourceList.Fields["ID"].InternalName;
                    primaryCol.Indexed = true;
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;

                    string strSecondaryCol = dependentList.Fields.AddDependentLookup("Last Name", primaryCol.Id);
                    SPFieldLookup secondaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strSecondaryCol);
                    secondaryCol.LookupField = sourceList.Fields["Last Name"].InternalName;
            Console.Write("\nPress ENTER to continue...");