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)

'Declaration
Public Property PrimaryFieldId As String
	Get
	Set
'Usage
Dim instance As SPFieldLookup
Dim value As String

value = instance.PrimaryFieldId

instance.PrimaryFieldId = value

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.

ExceptionCondition
SPException

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

NotSupportedException

The field belongs to an external list.

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.

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.

Imports System
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("Pending Orders")

                For Each item As SPField In list.Fields
                    If TypeOf item Is SPFieldLookup Then
                        Dim field As SPFieldLookup = DirectCast(item, SPFieldLookup)

                        If Not String.IsNullOrEmpty(field.LookupList) AndAlso Not String.IsNullOrEmpty(field.LookupField) Then
                            ' Is this the primary or secondary field for a list relationship?
                            Dim strRelationship As String = If(field.IsRelationship, "Primary", "Secondary")

                            ' Print the display name of the field.
                            Console.WriteLine(vbLf & "Field: {0} ({1} Field)", field.Title, strRelationship)

                            ' Is this a secondary field in a list relationship?
                            If field.IsDependentLookup Then
                                Dim primaryField As SPField = list.Fields(New Guid(field.PrimaryFieldId))
                                Console.WriteLine("Primary Field: {0}", primaryField.Title)
                            End If
                        End If
                    End If
                Next
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module
Show: