共用方式為


SPFieldCollection.AddDependentLookup method

新增取決於主要查閱欄位 (相對於取得其資訊的清單所保有的關係) 的次要查閱欄位。

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

Syntax

'宣告
Public Function AddDependentLookup ( _
    displayName As String, _
    primaryLookupFieldId As Guid _
) As String
'用途
Dim instance As SPFieldCollection
Dim displayName As String
Dim primaryLookupFieldId As Guid
Dim returnValue As String

returnValue = instance.AddDependentLookup(displayName, _
    primaryLookupFieldId)
public string AddDependentLookup(
    string displayName,
    Guid primaryLookupFieldId
)

參數

  • displayName
    Type: System.String

    第二個 [查詢] 欄位顯示名稱。

傳回值

Type: System.String
欄位的內部名稱。

Exceptions

Exception Condition
ArgumentNullException

displayName參數中傳遞的值是空字串或 null。

ArgumentNullException

primaryLookupFieldId參數中傳遞的值會是 Guid.Empty 則為 null。

SPException

primaryLookupFieldId參數中傳遞的值所識別的欄位不存在,不是型別SPFieldLookup或它的IsDependentLookup屬性會傳回true。

備註

在多個資料行對應中,主要的欄位會是值的建立做為來源的 [查詢] 欄位清單與關係的SPFieldLookup物件。一或多個次要的欄位然後可以依賴其 [來源] 清單的關聯性的主要欄位。[來源] 清單所知的主欄位中 ;也就是您可以發現主欄位,藉由檢查 [來源] 清單的GetRelatedFields()方法所傳回集合中的物件。主欄位中,依序所知的任何依存於它 ; 的欄位您可以藉由呼叫主欄位的GetDependentLookupInternalNames()方法發現相依的欄位。

若要建立多個資料行對應,開始建立主欄位,藉由呼叫AddLookup(String, Guid, Boolean)方法。您可以再使用AddDependentLookup方法來建立一或多個主要的欄位而定的第二個欄位。

Examples

下列範例是主控台應用程式,取得集合的暫止的訂單清單相關聯的欄位,並加入 「 查閱 」 欄位命名為客戶編號,指向 [客戶] 清單中的 [識別碼] 欄位。然後程式碼會建立取決於其關聯到客戶清單的 [客戶識別碼] 欄位的第二個欄位。

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 sourceList = site.Lists["Customers"];
                    SPList dependentList = site.Lists["Pending Orders"];

                    // Create the primary column.
                    string strPrimaryCol = dependentList.Fields.AddLookup("Customer ID", sourceList.ID, true);
                    SPFieldLookup primaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strPrimaryCol);
                    primaryCol.LookupField = sourceList.Fields["ID"].InternalName;
                    primaryCol.Indexed = true;
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                    primaryCol.Update();

                    // Create the secondary column.
                    string strSecondaryCol = dependentList.Fields.AddDependentLookup("Last Name", primaryCol.Id);
                    SPFieldLookup secondaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strSecondaryCol);
                    secondaryCol.LookupField = sourceList.Fields["Last Name"].InternalName;
                    secondaryCol.Update();

                    // Make the primary column the first one on New and Edit forms.
                    SPContentType contentType = dependentList.ContentTypes[0];
                    SPFieldLinkCollection fieldRefs = contentType.FieldLinks;
                    fieldRefs.Reorder(new[] { primaryCol.InternalName });
                    contentType.Update();
                    

                    // Add the columns to the default view.
                    SPView view = dependentList.DefaultView;
                    if (view != null)
                    {
                        SPViewFieldCollection viewFields = view.ViewFields;
                        if (viewFields != null)
                        {
                            viewFields.Add(primaryCol);
                            viewFields.Add(secondaryCol);
                            viewFields.MoveFieldTo(primaryCol.InternalName, 0);
                            viewFields.MoveFieldTo(secondaryCol.InternalName, 1);
                            view.Update();
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Namespace RelatedLists
    Class Program
        Shared Sub Main(ByVal args As String())
            Using siteCollection As New SPSite("https://localhost")
                Using site As SPWeb = siteCollection.OpenWeb()
                    Dim sourceList As SPList = site.Lists("Customers")
                    Dim dependentList As SPList = site.Lists("Pending Orders")

                    ' Create the primary column.
                    Dim strPrimaryCol As String = dependentList.Fields.AddLookup("Customer ID", sourceList.ID, True)
                    Dim primaryCol As SPFieldLookup = DirectCast(dependentList.Fields.GetFieldByInternalName(strPrimaryCol), SPFieldLookup)
                    primaryCol.LookupField = sourceList.Fields("ID").InternalName
                    primaryCol.Indexed = True
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict
                    primaryCol.Update()

                    ' Create the secondary column.
                    Dim strSecondaryCol As String = dependentList.Fields.AddDependentLookup("Last Name", primaryCol.Id)
                    Dim secondaryCol As SPFieldLookup = DirectCast(dependentList.Fields.GetFieldByInternalName(strSecondaryCol), SPFieldLookup)
                    secondaryCol.LookupField = sourceList.Fields("Last Name").InternalName
                    secondaryCol.Update()

                    'Make the primary column the first one on New and Edit forms.
                    Dim contentType As SPContentType = dependentList.ContentTypes(0)
                    Dim fieldRefs As SPFieldLinkCollection = contentType.FieldLinks
                    fieldRefs.Reorder(New String() {primaryCol.InternalName})
                    contentType.Update()

                    ' Add the columns to the default view.
                    Dim view As SPView = dependentList.DefaultView
                    If view <> Nothing Then
                        Dim viewFields As SPViewFieldCollection = view.ViewFields
                        If viewFields <> Nothing Then
                            viewFields.Add(primaryCol)
                            viewFields.Add(secondaryCol)
                            viewFields.MoveFieldTo(primaryCol.InternalName, 0)
                            viewFields.MoveFieldTo(secondaryCol.InternalName, 1)
                            view.Update()
                        End If
                    End If
                End Using
            End Using
            Console.Write(vbLf & "Press ENTER to continue...")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

請參閱

參照

SPFieldCollection class

SPFieldCollection members

Microsoft.SharePoint namespace

AddLookup(String, Guid, Boolean)

IsDependentLookup

GetDependentLookupInternalNames()

GetRelatedFields()