Share via


SPFieldLookup.LookupList property

Gets or sets the string representation of the GUID that identifies the list that is the source of this field's value.

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

Syntax

public string LookupList { get; set; }

Property value

Type: System.String
The string representation of the GUID that identifies the list that is the source of information for the field. An empty string is returned if the field does not point to a list.

Exceptions

Exception Condition
SPException

The property has already been set. You cannot change the lookup list after the LookupList property is set.

Remarks

This property returns the string representation of the source list's ID property. The LookupList property is set automatically when you create an SPFieldLookup object by calling the AddLookup or AddDependentLookup method of the SPFieldCollection object that represents the field collection for the list where you want to add the lookup column.

Important

Once a lookup column is established, the target list cannot be changed.

Examples

The following example is a console application that enumerates the fields associated with a list, looking for fields of type SPFieldLookup. When it finds a field of that type, the application uses the value of the LookupList property to get a reference to the list that the lookup field points to and then the value of the LookupField property to get the field in the target list that the lookup field points to.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList list = site.Lists["Pending Orders"];

                    foreach (SPField item in list.Fields)
                    {
                        if (item is SPFieldLookup)
                        {
                            SPFieldLookup field = (SPFieldLookup)item;

                            if (!String.IsNullOrEmpty(field.LookupList) && !String.IsNullOrEmpty(field.LookupField))
                            {
                                // Is this the primary or secondary field for a list relationship?
                                string strRelationship = field.IsRelationship ? "Primary":"Secondary";

                                // Print the display name of the field.
                                Console.WriteLine("\nField: {0} ({1} Field)", field.Title, strRelationship);

                                // Is this a secondary field in a list relationship?
                                if (field.IsDependentLookup)
                                {
                                    SPField primaryField = list.Fields[new Guid(field.PrimaryFieldId)];
                                    Console.WriteLine("Primary Field: {0}", primaryField.Title);
                                }

                                // Get the site where the target list is located.
                                using (SPWeb targetSite = siteCollection.AllWebs[field.LookupWebId])
                                {
                                    // Get the name of the list where this field gets information.
                                    SPList targetList = targetSite.Lists[new Guid(field.LookupList)];
                                    Console.WriteLine("Related list: {0}", targetList.Title);

                                    // Get the name of the field where this field gets information.
                                    SPField targetField = targetList.Fields.GetFieldByInternalName(field.LookupField);
                                    Console.WriteLine("Related field: {0}", targetField.Title);
                                }
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

See also

Reference

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace

LookupField

ID

AddLookup(String, Guid, Boolean)

AddDependentLookup(String, Guid)