SPList.GetRelatedFields method
另一個指向此清單中的欄位的清單中,會傳回具有查閱欄位的相關資訊的物件的集合。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Function GetRelatedFields As SPRelatedFieldCollection
'用途
Dim instance As SPList
Dim returnValue As SPRelatedFieldCollection
returnValue = instance.GetRelatedFields()
public SPRelatedFieldCollection GetRelatedFields()
傳回值
Type: Microsoft.SharePoint.SPRelatedFieldCollection
SPRelatedField 物件的集合。
備註
Microsoft SharePoint Foundation中使用者建立上一個清單,其值從另一個清單上的欄位的查閱欄位時,會成為相關兩個清單。您可以想像所提供的父項的資料清單和資料,做為子查詢的清單。您可以探索子清單中的欄位所代表的父清單的SPList物件上呼叫GetRelatedFields方法而定上層清單中的資訊。此方法會傳回具有下列內容的SPRelatedField物件的集合:
RelationshipDeleteBehavior。取得定義關聯性的資料完整性限制。
最後一個屬性, RelationshipDeleteBehavior,代表清單關聯的重要環節。會建立查閱] 欄位中的,透過使用者介面或透過物件模型,使用者可以設定從一個清單中刪除如何影響刪除來自其他的條件約束。例如,使用者可以指定從父項的刪除清單下層 ;重疊顯示也就是說,當項目已從父清單中刪除,也會刪除子清單上所有相關的項目。使用者也可以指定從上層清單的刪除作業都是限制;無法從父清單中刪除項目,如果相關項目存在子清單上。如需詳細資訊,請參閱 < SPRelationshipDeleteBehavior列舉。
注意事項 |
---|
若要限制刪除,使用者必須具有SPBasePermissions。在上層清單ManageLists權限。 |
在SharePoint Foundation使用者也可以建立多欄查閱。在此例中子清單已定義的關聯性與父] 清單中,主要欄,且其具有一或多個第二欄讀取父清單上的欄位的值,但取決於清單關聯的 [主要] 欄。SPRelatedField物件含有主要欄的相關資訊。若要取得第二欄的相關資訊,請呼叫SPFieldLookup物件,表示主要欄GetDependentLookupInternalNames方法。如需詳細資訊,請參閱 < GetDependentLookupInternalNames()方法。
Examples
下列範例會為主控台應用程式,取得從客戶清單項目,並在其他網站集合中的清單中尋找相關的項目。最粗大部分的工作,就可以應用程式的PrintRelatedItems方法,使用 [查閱] 清單中的項目和資訊從SPRelatedField物件來建構對相關清單的查詢。
using System;
using Microsoft.SharePoint;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite("https://localhost"))
{
using (SPWeb site = siteCollection.OpenWeb())
{
int customerID = 1;
SPList list = site.Lists.TryGetList("Customers");
if (list != null)
{
// Get a customer record.
SPListItem customerRecord = null;
try
{
customerRecord = list.GetItemById(customerID);
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
// Print related items.
if (customerRecord != null)
{
Console.WriteLine("Customer: {0} {1}",
customerRecord[SPBuiltInFieldId.FirstName], customerRecord[SPBuiltInFieldId.Title]);
// Get related list items.
SPRelatedFieldCollection relatedFields = list.GetRelatedFields();
foreach (SPRelatedField fieldInfo in relatedFields)
{
using (SPWeb relatedSite = siteCollection.AllWebs[fieldInfo.WebId])
{
PrintRelatedItems(customerRecord, relatedSite, fieldInfo);
}
}
}
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
static void PrintRelatedItems(SPListItem match, SPWeb site, SPRelatedField fieldInfo)
{
SPList targetList = fieldInfo.LookupList;
SPList relatedList = site.Lists[fieldInfo.ListId];
SPFieldLookup relatedField = relatedList.Fields[fieldInfo.FieldId] as SPFieldLookup;
SPField targetField = targetList.Fields.GetFieldByInternalName(relatedField.LookupField);
object value = match[targetField.Id];
SPQuery q = new SPQuery();
q.Query = string.Format(@"<Where>
<Eq>
<FieldRef Name=""{0}""/>
<Value Type=""{1}"">{2}</Value>
</Eq>
</Where>", relatedField.InternalName, value.GetType(), value);
SPListItemCollection items = relatedList.GetItems(q);
if (items.Count > 0)
{
Console.WriteLine("\n{0} has {1} related items:", relatedList.Title, items.Count);
foreach (SPListItem item in items)
Console.WriteLine(item.DisplayName);
}
}
}
}
Imports System
Imports Microsoft.SharePoint
Module ConsoleApp
Sub Main()
Using siteCollection As New SPSite("https://localhost")
Using site As SPWeb = siteCollection.OpenWeb()
Dim customerID As Integer = 1
Dim list As SPList = site.Lists.TryGetList("Customers")
If list IsNot Nothing Then
' Get a customer record.
Dim customerRecord As SPListItem = Nothing
Try
customerRecord = list.GetItemById(customerID)
Catch ex As ArgumentException
Console.WriteLine(ex.Message)
End Try
' Print related items.
If customerRecord IsNot Nothing Then
Console.WriteLine("Customer: {0} {1}", _
customerRecord(SPBuiltInFieldId.FirstName), customerRecord(SPBuiltInFieldId.Title))
' Get related list items.
Dim relatedFields As SPRelatedFieldCollection = list.GetRelatedFields()
For Each fieldInfo As SPRelatedField In relatedFields
Using relatedSite As SPWeb = siteCollection.AllWebs(fieldInfo.WebId)
PrintRelatedItems(customerRecord, relatedSite, fieldInfo)
End Using
Next
End If
End If
End Using
End Using
Console.Write(vbLf & "Press ENTER to continue...")
Console.ReadLine()
End Sub
Sub PrintRelatedItems(ByVal match As SPListItem, ByVal site As SPWeb, ByVal fieldInfo As SPRelatedField)
Dim targetList As SPList = fieldInfo.LookupList
Dim relatedList As SPList = site.Lists(fieldInfo.ListId)
Dim relatedField As SPFieldLookup = TryCast(relatedList.Fields(fieldInfo.FieldId), SPFieldLookup)
Dim targetField As SPField = targetList.Fields.GetFieldByInternalName(relatedField.LookupField)
Dim value As Object = match(targetField.Id)
Dim q As New SPQuery()
q.Query = String.Format( _
"<Where><Eq><FieldRef Name=""{0}""/><Value Type=""{1}"">{2}</Value></Eq></Where>", _
relatedField.InternalName, value.GetType(), value)
Dim items As SPListItemCollection = relatedList.GetItems(q)
If items.Count > 0 Then
Console.WriteLine(vbLf & "{0} has {1} related items:", relatedList.Title, items.Count)
For Each item As SPListItem In items
Console.WriteLine(item.DisplayName)
Next
End If
End Sub
End Module