SPFieldLookup.PrimaryFieldId property

Gets or sets a string representation of the GUID that identifies the primary lookup field if the field is a dependent lookup field.

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

Syntax

public string PrimaryFieldId { get; set; }

Property value

Type: System.String
If the field is a secondary field in a multi-column lookup, this property returns the string representation of the GUID that identifies the primary lookup field; otherwise, it returns an empty string.

Exceptions

Exception Condition
SPException

You cannot change the value of this property after it has been set.

NotSupportedException

The field belongs to an external list.

Remarks

When you create a multiple column lookup, the primary field is the SPFieldLookup object that establishes the relationship with the source list. One or more secondary fields depend on the primary field for the relationship to the source list.

If the current SPFieldLookup object's IsDependentLookup property returns true, then the current object is a secondary field and its PrimaryFieldId property will return the string representation of the GUID that identifies the primary lookup field on which it depends.

Examples

The following example is a console application that examines the collection of fields associated with a list, looking for SPFieldLookup objects. When it finds one, code in the application determines whether the object represents a primary or secondary column. If the object represents a secondary column, the code uses the value returned by the PrimaryFieldId property to get the display name of the primary column.

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["Complete 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);
                                }
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

See also

Reference

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace

IsDependentLookup

AddDependentLookup(String, Guid)