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)

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

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

Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using siteCollection As New SPSite("http://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."
                            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
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
    End Sub
End Module