共用方式為


SPFieldLookup.PrimaryFieldId property

取得或設定識別主要的查閱欄位,如果欄位是相依的查閱欄位的 GUID 的字串表示。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Property PrimaryFieldId As String
    Get
    Set
'用途
Dim instance As SPFieldLookup
Dim value As String

value = instance.PrimaryFieldId

instance.PrimaryFieldId = value
public string PrimaryFieldId { get; set; }

Property value

Type: System.String
如果欄位是多個資料行對應中的第二個欄位,此屬性傳回的字串表示的 GUID 來識別主查詢] 欄位中 ;否則,它會傳回空字串。

Exceptions

Exception Condition
SPException

已設定之後,您無法變更這個屬性的值。

NotSupportedException

欄位屬於外部的清單。

備註

當您建立多個資料行對應時,主要的欄位會是建立來源] 清單中的色彩與關係的SPFieldLookup物件。一或多個次要的欄位取決於 [來源] 清單的關聯性的主要欄位。

如果目前的SPFieldLookup物件的IsDependentLookup屬性會傳回true,然後目前的物件是次要的欄位,而它的PrimaryFieldId屬性將會傳回 GUID 的字串表示,會識別它所依賴的主要對應欄位。

Examples

下列範例是主控台應用程式,會檢查欄位] 清單中,尋找SPFieldLookup物件相關聯的集合。當它會尋找第一時,應用程式中的程式碼會判斷物件是否表示主要或次要的資料行。如果該物件代表第二個資料行,程式碼會使用PrimaryFieldId屬性所傳回的值來取得主資料行的顯示名稱。

using System;
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["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...");
            Console.ReadLine();
        }
    }
}
Imports System
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("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

請參閱

參照

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace

IsDependentLookup

AddDependentLookup(String, Guid)