SPFieldCollection.AddLookup method (String, Guid, Boolean)
建立查閱欄位的其中一個清單的欄位集合中該點來在相同網站中的另一份清單的集合中的欄位。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
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
Type: System.String字串,指定欄位的顯示名稱。
lookupListId
Type: System.Guid指定的 [查詢] 欄位的 [目標] 清單的 GUID。
bRequired
Type: System.Booleantrue 要求欄位包含的值。否則, false.
傳回值
Type: System.String
字串,包含用於欄位的內部名稱。您可以將這個值傳遞至GetFieldByInternalName(String)方法來擷取新的欄位。會傳回該欄位為的型別SPFieldLookup。
備註
這個方法會建立目前清單的欄位集合中的型別SPFieldLookup欄位。一個清單中的查閱欄位接受它的值從另一個清單中的欄位在lookupListId參數中指定的目標清單。集合中加入 [查詢] 欄位之後,您應該從集合中擷取,並設定LookupField屬性,以識別在 [目標] 清單中的 [來源] 欄位。
[查詢] 欄位值的來源的目標清單所知的 [查詢] 欄位中 ;也就是您可以藉由檢查目標清單的GetRelatedFields()方法所傳回集合中的物件會發現 [查詢] 欄位。
目前的使用者必須具有SPBasePermissions。當您呼叫這個方法中的 [目標] 清單上的ManageLists使用權限。
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"];
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
請參閱
參照
Microsoft.SharePoint namespace
GetFieldByInternalName(String)