GetDependentLookupInternalNames Method

SPFieldLookup.GetDependentLookupInternalNames Method

Returns the internal names of all secondary lookup fields that are dependent on a primary lookup field.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

public List<string> GetDependentLookupInternalNames()

Return Value

Type: System.Collections.Generic.List<String>
The internal names of dependent lookups as a list of strings. If there are no dependent lookup fields, the list that is returned has zero elements (the Count property returns 0).

An SPFieldLookup object represents the primary column in a list relationship if the IsRelationship property returns true. An SPFieldLookup object represents a secondary column in a list relationship if the IsDependentLookup property returns true.

The following example is a console application that enumerates the fields collection of a list, looking for fields that represent the primary column in a list relationship. For each primary column that the application finds, it lists any secondary columns that depend on it.

using System;
using System.Collections.Generic;
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["Addresses"];

                    foreach (SPField field in list.Fields)
                        SPFieldLookup lookupField = field as SPFieldLookup;
                        if (lookupField != null && lookupField.IsRelationship)
                            // Print the display name of the field.
                            Console.WriteLine("\nPrimary column: {0}", lookupField.Title);

                            // Get any dependent fields.
                            List<string> internalNames = lookupField.GetDependentLookupInternalNames();

                            // Convert internal names to display names.
                            string[] displayNames = new string[internalNames.Count];
                            if (internalNames.Count == 0)
                                displayNames[0] = "No secondary columns.";
                                for (int i = 0; i < internalNames.Count; i++)
                                    displayNames[i] = list.Fields.GetFieldByInternalName(internalNames[i]).Title;

                                // Print the display names.
                                Console.Write("Secondary columns: ");
                                Console.WriteLine(string.Join(", ", displayNames));
            Console.Write("\nPress ENTER to continue...");
© 2016 Microsoft