Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SPFieldLookup.IsDependentLookup property

Gets a Boolean value that indicates whether the field is a secondary lookup field that depends on a primary field for its relationship with the lookup list.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
public bool IsDependentLookup { get; }

Property value

Type: System.Boolean
true if the lookup field is a dependent lookup field; otherwise, false.

When this property returns true, the current SPFieldLookup object represents a secondary column in a multiple column lookup. In this case, the object's PrimaryFieldId property has a value that is the string representation of a GUID that identifies the object that represents the primary column on which the secondary column depends for its relationship to the lookup list.

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("http://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...");
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.