SPFieldLookup.LookupField 属性
获取或设置此字段的值的源的相关列表中该字段的内部名称。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Property LookupField As String
Get
Set
用法
Dim instance As SPFieldLookup
Dim value As String
value = instance.LookupField
instance.LookupField = value
public string LookupField { get; set; }
属性值
类型:System.String
一个字符串,包含该字段的内部名称。
备注
通过调用AddLookup方法向列表的集合添加查阅字段后,应从集合中检索查阅字段,并通过设置LookupField属性确定在目标列表中的源字段。设置LookupField属性时必须调用Update方法以更改将在数据库中生效。
LookupField 属性的目标必须为下列字段类型之一:
SPFieldType.Counter
SPFieldType.DateTime
SPFieldType.Number
SPFieldType.Text
此外, SPFieldType.Computed允许作为一个目标如果查找已启用。有关详细信息,请参阅SPFieldComputed类的EnableLookup属性。如果输出是文本, SPFieldType.Calculated字段类型可以是目标。有关详细信息,请参阅SPFieldCalculated类的OutputType属性。
示例
以下示例是在"客户"列表和"待定订单"列表之间创建关系的控制台应用程序。该应用程序调用 AddLookup 方法以将名为"客户 ID"的查阅字段添加到"待定订单"列表,并将该字段指向"客户"列表上的 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 lookupList = site.Lists.TryGetList("Customers");
SPList relatedList = site.Lists.TryGetList("Pending Orders");
if (lookupList != null && relatedList != null)
{
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();
}
}
}
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
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()
End If
End Using
End Using
Console.Write(vbLf & "Press ENTER to continue...")
Console.ReadLine()
End Sub
End Module