Share via


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

Syntax

'Declaration
Public Function GetDependentLookupInternalNames As List(Of String)
'Usage
Dim instance As SPFieldLookup
Dim returnValue As List(Of String)

returnValue = instance.GetDependentLookupInternalNames()
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).

Remarks

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.

Examples

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("https://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.";
                            }
                            else
                            {
                                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...");
            Console.ReadLine();
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using siteCollection As New SPSite("https://localhost")
            Using site As SPWeb = siteCollection.OpenWeb()
                Dim list As SPList = site.Lists("Addresses")

                For Each field As SPField In list.Fields
                    Dim lookupField As SPFieldLookup = TryCast(field, SPFieldLookup)
                    If lookupField IsNot Nothing AndAlso lookupField.IsRelationship Then
                        ' Print the display name of the field.
                        Console.WriteLine(vbLf & "Primary column: {0}", lookupField.Title)

                        ' Get any dependent fields.
                        Dim internalNames As List(Of String) = lookupField.GetDependentLookupInternalNames()

                        ' Convert internal names to display names.
                        Dim displayNames As String() = New String(internalNames.Count) {}
                        If internalNames.Count = 0 Then
                            displayNames(0) = "No secondary columns."
                        Else
                            Dim i As Integer = 0
                            While i < internalNames.Count
                                displayNames(i) = list.Fields.GetFieldByInternalName(internalNames(i)).Title
                                i = i + 1
                            End While

                            ' Print the display names.
                            Console.Write("Secondary columns: | ")
                            Console.WriteLine(String.Join(" | ", displayNames))
                        End If
                    End If
                Next
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module

See Also

Reference

SPFieldLookup Class

SPFieldLookup Members

Microsoft.SharePoint Namespace

IsRelationship

IsDependentLookup