共用方式為


SPFieldLookup class

代表 「 查閱 」 欄位。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPField
    Microsoft.SharePoint.SPFieldLookup
      Microsoft.SharePoint.Applications.GroupBoard.SPFieldFacilities
      Microsoft.SharePoint.SPFieldUser

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

Syntax

'宣告
Public Class SPFieldLookup _
    Inherits SPField
'用途
Dim instance As SPFieldLookup
public class SPFieldLookup : SPField

備註

[查詢] 欄位會從另一個清單中的欄位中取得其值。提供 [查詢] 欄位中的資料清單會呼叫[查詢] 清單。若要取得 [查閱] 清單的參考,存取SPFieldLookup物件的LookupList屬性。如果要判斷哪一個 [查詢] 清單中的欄位提供資訊,存取LookupField屬性。

注意事項注意事項

SPFieldLookup類別對應至宣告式的 XML,透過Type屬性的欄位項目中指定的Lookup資料型別。List的Field項目對應至SPFieldLookup類別的 [ LookupList ] 屬性。

雖然SPFieldLookup類別沒有建構函式,它通常是比較方便呼叫SPFieldCollection類別的AddLookup方法。AddLookup方法具有多載,您可以用來建立查閱到的清單中不同的 Web 站台與您要在其中建立SPFieldLookup物件。

Microsoft SharePoint Foundation支援多個資料行對應。在此情況下,資料行建立與 [查閱] 清單的關係,指向 [目標] 清單中的欄位和一或多個次要的資料行指到 [目標] 清單中其他欄位。您可以藉由呼叫SPFieldCollection類別的AddDependentLookup方法來建立第二個資料行的 [查詢] 欄位。第二個資料行, IsDependentLookup屬性會傳回true。針對主要的資料行中, IsDependentLookup會傳回false和IsRelationship屬性會傳回true。

SharePoint Foundation藉由讓設定刪除條件約束在查閱清單的查閱欄位的清單項目支援參考完整性。您這麼做,透過RelationshipDeleteBehavior屬性,會採用下列的SPRelationshipDeleteBehavior列舉型別值之一:

  • Cascade。從 [查詢] 清單中刪除項目會導致所有相關的項目從清單中包含 [查詢] 欄位中刪除。

  • Restrict。防止在查閱清單的項目相關的項目存在於清單中包含 [查詢] 欄位上遭到刪除。

  • None。(預設值) 沒有條件約束。

若要指定Cascade或Restrict,使用者必須SPBasePermissions。在查閱清單的ManageLists使用權限。此外, Cascade和Restrict都需要 [查詢] 欄位進行索引。之前將RelationshipDeleteBehavior屬性設定為其中一個這些值,第一次設定Indexed屬性為true。

注意事項注意事項

您不能設定可讓多個值,[查詢] 欄位刪除條件約束,也是不可能的查閱清單是否位於另一個網站。

在SharePoint Foundation,在相關的清單中的 [查詢] 欄位是從 [查閱] 清單 (該查詢的目標) 透過SPList類別的GetRelatedFields()方法設定為可探索的。GetRelatedFields方法不會有可以讓您篩選指定特定的刪除條件約束的查閱欄位的多載。

Lookup Field Values

根據預設,[查詢] 欄位會包含只有一個值。在此情況下,欄位值是物件的型別String,而字串具有下列格式:

Id;#Value

Id是 [查詢] 欄位所指向的點之清單項目的識別項。為 [查詢] 欄位會指向欄位的值。字串的兩個部分是以";#"的字元分隔。如果包含一個分號 (;),它逸出的以分號 ("; ;")。

例如,如果 [查詢] 欄位會指向第三個清單項目,並指向 [查詢] 欄位的欄位值是 「 我 gnaw 在舊的輪胎。會傳回它可強化我顎骨 」,然後將下列字串:

3;#I gnaw on old tires;;it strengthens my jaw

您可以自行剖析此字串,或傳遞做為引數的SPFieldLookupValue物件的建構函式。您可以取得從SPFieldLookupValue物件的 [ LookupId ] 屬性和欄位的值的清單項目識別項清單項目,從LookupValue屬性中。

[查詢] 欄位可以有多個值,如果AllowMultipleValues屬性會傳回true。在此情況下,欄位值是會被 box 為Object的型別SPFieldLookupValueCollection的物件。您可以先轉型為SPFieldLookupValueCollection的未經處理的欄位值,然後列舉集合中的SPFieldLookupValue物件,以取得所有的值。

Notes to inheritors

SharePoint Foundation,在自訂欄位類型為基礎的資料行是唯讀,在 [資料工作表檢視中。

Examples

下列範例是主控台應用程式,建立客戶清單和暫止的訂單清單之間的關係。應用程式呼叫AddLookup方法,以新增名為暫止的訂單清單客戶識別碼的主要的查閱欄位,並指出在客戶清單上的 [識別碼] 欄位的欄位。新的 [客戶識別碼] 欄位是索引,而且設定限制從 [查詢] 清單刪除。

建立主要的查閱欄位之後, 在應用程式會建立三個次要的欄位命名為名字、 姓氏和電話。應用程式會藉由呼叫AddDependentLookup方法代表物件的欄位集合的暫止的訂單清單建立這些欄位。

using System;
using Microsoft.SharePoint;

namespace RelatedLists
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList lookupList = site.Lists.TryGetList("Customers");
                    SPList relatedList = site.Lists.TryGetList("Pending Orders");

                    if (lookupList != null && relatedList != null)
                    {
                        // Create the primary column.
                        string strPrimaryCol = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, true);
                        SPFieldLookup primaryCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPrimaryCol);

                        primaryCol.LookupField = lookupList.Fields["ID"].InternalName;
                        primaryCol.Indexed = true;
                        primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                        primaryCol.Update();


                        // Create the secondary columns.

                        string strFirstNameCol = relatedList.Fields.AddDependentLookup("First Name", primaryCol.Id);
                        SPFieldLookup firstNameCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strFirstNameCol);
                        firstNameCol.LookupField = lookupList.Fields["First Name"].InternalName;
                        firstNameCol.Update();

                        string strLastNameCol = relatedList.Fields.AddDependentLookup("Last Name", primaryCol.Id);
                        SPFieldLookup lastNameCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strLastNameCol);
                        lastNameCol.LookupField = lookupList.Fields["Last Name"].InternalName;
                        lastNameCol.Update();

                        string strPhoneCol = relatedList.Fields.AddDependentLookup("Phone", primaryCol.Id);
                        SPFieldLookup phoneCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPhoneCol);
                        phoneCol.LookupField = lookupList.Fields["Phone"].InternalName;
                        phoneCol.Update();
                    }
                }
            }
            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 lookupList As SPList = site.Lists.TryGetList("Customers")
                Dim relatedList As SPList = site.Lists.TryGetList("Pending Orders")

                If lookupList IsNot Nothing AndAlso relatedList IsNot Nothing Then
                    ' Create the primary column.
                    Dim strPrimaryCol As String = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, True)
                    Dim primaryCol As SPFieldLookup = _
                        DirectCast(relatedList.Fields.GetFieldByInternalName(strPrimaryCol), SPFieldLookup)

                    primaryCol.LookupField = lookupList.Fields("ID").InternalName
                    primaryCol.Indexed = True
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict
                    primaryCol.Update()


                    ' Create the secondary columns.

                    Dim strFirstNameCol As String = relatedList.Fields.AddDependentLookup("First Name", primaryCol.Id)
                    Dim firstNameCol As SPFieldLookup = _
                        DirectCast(relatedList.Fields.GetFieldByInternalName(strFirstNameCol), SPFieldLookup)
                    firstNameCol.LookupField = lookupList.Fields("First Name").InternalName
                    firstNameCol.Update()

                    Dim strLastNameCol As String = relatedList.Fields.AddDependentLookup("Last Name", primaryCol.Id)
                    Dim lastNameCol As SPFieldLookup = _
                        DirectCast(relatedList.Fields.GetFieldByInternalName(strLastNameCol), SPFieldLookup)
                    lastNameCol.LookupField = lookupList.Fields("Last Name").InternalName
                    lastNameCol.Update()

                    Dim strPhoneCol As String = relatedList.Fields.AddDependentLookup("Phone", primaryCol.Id)
                    Dim phoneCol As SPFieldLookup = _
                        DirectCast(relatedList.Fields.GetFieldByInternalName(strPhoneCol), SPFieldLookup)
                    phoneCol.LookupField = lookupList.Fields("Phone").InternalName
                    phoneCol.Update()
                End If
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module

下一個範例示範如何解譯查閱欄位的值。

範例程式碼會尋找根網站,網站集合中的問題清單。問題清單型別是這裡選擇,因為它有好幾個查閱欄位。您可以修改範例藉由選取另一種清單類型。找不到清單,範例會選取第一個 [查詢] 欄位,在該清單中的欄位集合中找到。然後程式碼會逐一查看清單項目,每個項目查詢欄位的值列印資訊。

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = null;
                    foreach (SPList webList in web.Lists)
                    {
                        if (webList.BaseType == SPBaseType.Issue)
                        {
                            list = webList;
                            break;
                        }
                    }
                    if (list != null)
                    {
                        Guid fieldId = Guid.Empty;
                        foreach (SPField field in list.Fields)
                        {
                            if (field is SPFieldLookup)
                            {
                                fieldId = field.Id;
                                break;
                            }
                        }

                        if (fieldId != Guid.Empty)
                        {
                            SPFieldLookup lookupField = list.Fields[fieldId] as SPFieldLookup;
                            Console.WriteLine("Lookup field: {0}", lookupField.Title);

                            foreach (SPListItem item in list.Items)
                            {
                                object rawValue = item[fieldId];
                                if (rawValue != null)
                                {
                                    Console.WriteLine("\nItem: {0}. {1}", item.ID, item.Title);
                                    Console.WriteLine("Raw value: {0}", rawValue);

                                    if (lookupField.AllowMultipleValues)
                                    {
                                        SPFieldLookupValueCollection values = item[fieldId] as SPFieldLookupValueCollection;
                                        foreach (SPFieldLookupValue value in values)
                                        {
                                            PrintValue(value);
                                        }
                                    }
                                    else
                                    {
                                        SPFieldLookupValue value = new SPFieldLookupValue(rawValue.ToString());
                                        PrintValue(value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }

        public static void PrintValue(SPFieldLookupValue value)
        {
            Console.WriteLine("Lookup item ID: {0}", value.LookupId);
            Console.WriteLine("Lookup item value: {0}", value.LookupValue);
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main(ByVal args As String())
        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.RootWeb
                Dim list As SPList = Nothing
                For Each webList As SPList In web.Lists
                    If webList.BaseType = SPBaseType.Issue Then
                        list = webList
                        Exit For
                    End If
                Next
                If list <> Nothing Then
                    Dim fieldId As Guid = Guid.Empty
                    For Each field As SPField In list.Fields
                        If TypeOf field Is SPFieldLookup Then
                            fieldId = field.Id
                            Exit For
                        End If
                    Next

                    If fieldId <> Guid.Empty Then
                        Dim lookupField As SPFieldLookup = TryCast(list.Fields(fieldId), SPFieldLookup)
                        Console.WriteLine("Lookup field: {0}", lookupField.Title)

                        For Each item As SPListItem In list.Items
                            Dim rawValue As Object = item(fieldId)
                            If rawValue <> Nothing Then
                                Console.WriteLine(vbLf & "Item: {0}. {1}", item.ID, item.Title)
                                Console.WriteLine("Raw value: {0}", rawValue)

                                If lookupField.AllowMultipleValues Then
                                    Dim values As SPFieldLookupValueCollection = TryCast(item(fieldId), _
                                        SPFieldLookupValueCollection)
                                    For Each value As SPFieldLookupValue In values
                                        PrintValue(value)
                                    Next
                                Else
                                    Dim value As New SPFieldLookupValue(rawValue.ToString())
                                    PrintValue(value)
                                End If
                            End If
                        Next
                    End If
                End If
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    Sub PrintValue(ByVal value As SPFieldLookupValue)
        Console.WriteLine("Lookup item ID: {0}", value.LookupId)
        Console.WriteLine("Lookup item value: {0}", value.LookupValue)
    End Sub

End Module

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPFieldLookup members

Microsoft.SharePoint namespace

GetRelatedFields()

SPRelatedField

SPRelationshipDeleteBehavior