SPFieldCollection.AddDependentLookup 方法

添加一个依赖主查阅字段的辅助查阅字段,以便与它从中获取信息的列表形成关系。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
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
    类型:System.String

    显示名称为辅助的查阅字段的。

返回值

类型:System.String
该字段的内部名称。

异常

异常 条件
ArgumentNullException

在displayName参数中传递的值是空字符串或 null。

ArgumentNullException

在primaryLookupFieldId参数中传递的值是值或为空。

SPException

通过在primaryLookupFieldId参数中传递的值标识的字段不存在,则不属于类型SPFieldLookup或其IsDependentLookup属性返回true。

备注

在多个列查询中,主键字段是值的SPFieldLookup对象,建立了与查阅字段的源的列表之间的关系。一个或多个辅助字段然后可能取决于二者关系的源列表的主字段。源列表中都不知道的主字段。也就是说,可以通过检查此源列表中的GetRelatedFields()方法返回集合中的对象发现主字段。主字段,反过来,是意识到依赖于它 ; 任何字段通过调用该主键字段的GetDependentLookupInternalNames()方法,可以发现相关的字段。

若要创建多列查阅,首先通过调用AddLookup(String, Guid, Boolean)方法来创建主字段。然后可以使用AddDependentLookup方法来创建依赖于主字段的一个或多个辅助字段。

示例

下面的示例是一个控制台应用程序,获取与待处理订单列表相关联的字段的集合,并将添加查阅字段命名的客户 ID 指向标识号域中的客户列表。然后,该代码创建辅助字段依赖于其关系到客户列表中的客户 ID 字段。

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 类

SPFieldCollection 成员

Microsoft.SharePoint 命名空间

AddLookup(String, Guid, Boolean)

IsDependentLookup

GetDependentLookupInternalNames()

GetRelatedFields()