SPFieldCollection.AddLookup 方法 (String, Guid, Boolean)

指向同一个网站中的其他列表的集合中的字段的一个列表的字段集合中创建查阅字段。

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

语法

声明
Public Function AddLookup ( _
    displayName As String, _
    lookupListId As Guid, _
    bRequired As Boolean _
) As String
用法
Dim instance As SPFieldCollection
Dim displayName As String
Dim lookupListId As Guid
Dim bRequired As Boolean
Dim returnValue As String

returnValue = instance.AddLookup(displayName, _
    lookupListId, bRequired)
public string AddLookup(
    string displayName,
    Guid lookupListId,
    bool bRequired
)

参数

  • displayName
    类型:System.String

    一个字符串,指定该字段的显示名称。

  • lookupListId
    类型:System.Guid

    用于指定目标列表的查阅字段的 GUID。

  • bRequired
    类型:System.Boolean

    true 要求域必须包含值 ;否则为 false.

返回值

类型:System.String
一个字符串,包含用于字段的内部名称。您可以通过将此值传递给GetFieldByInternalName(String)方法中检索新字段。返回的字段属于类型SPFieldLookup

备注

此方法创建的字段类型SPFieldLookup的当前列表的字段集合中。在一个列表中的查阅字段获取其值的字段在另一个列表中,在lookupListId参数中指定的目标列表。向集合中添加查阅字段后,应从集合中检索,然后通过设置LookupField属性确定在目标列表中的源字段。

目标列表的查阅字段的值的源已意识到查阅字段。也就是说,可以通过检查此目标列表中的GetRelatedFields()方法返回集合中的对象发现查阅字段。

当前用户必须具有SPBasePermissions。ManageLists在调用此方法时,目标列表上的权限。

示例

下面的示例是一个控制台应用程序,获取与待处理订单列表相关联的字段的集合,并将添加查阅字段命名的客户 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"];

                    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();

                    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();
                }
            }
            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")

                    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()

                    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()
                End Using
            End Using
            Console.Write(vbLf & "Press ENTER to continue...")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

另请参阅

引用

SPFieldCollection 类

SPFieldCollection 成员

AddLookup 重载

Microsoft.SharePoint 命名空间

SPFieldLookup

LookupField

GetFieldByInternalName(String)

AddDependentLookup(String, Guid)

GetRelatedFields()