SPList.GetRelatedFields 方法
返回指向此列表中的字段的另一个列表中的信息的查阅字段的对象的集合。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Function GetRelatedFields As SPRelatedFieldCollection
用法
Dim instance As SPList
Dim returnValue As SPRelatedFieldCollection
returnValue = instance.GetRelatedFields()
public SPRelatedFieldCollection GetRelatedFields()
返回值
类型:Microsoft.SharePoint.SPRelatedFieldCollection
SPRelatedField 对象的集合。
备注
在Microsoft SharePoint Foundation两个列表成为相关的用户创建一个从另一个列表的字段中获取其值的列表上的查阅字段时。您可以将提供的数据与父列表和列表来查找作为子的数据。您可以发现子列表中的哪些字段依赖于父列表中的信息通过表示父列表的SPList对象上调用GetRelatedFields方法。该方法将返回具有以下属性的SPRelatedField对象的集合:
RelationshipDeleteBehavior。获取关系定义的数据完整性约束。
最后一个属性, RelationshipDeleteBehavior,代表列表关系的一个重要方面。创建查阅字段中的,通过用户界面或通过对象模型,用户可以设置从一个列表的删除操作如何影响从其他删除限制。例如,用户可以指定从父删除列表级联子级 ;即时从父列表中删除项目,还将删除子列表上的所有相关的项。用户还可以指定从父列表的删除操作是限制;如果子列表上存在相关的项不能从父列表中删除项目。有关详细信息,请参阅SPRelationshipDeleteBehavior枚举。
备注
为了限制删除,用户必须具有SPBasePermissions。ManageLists在父列表的权限。
在SharePoint Foundation用户还可以创建多列查阅。在这种情况下,子列表具有定义的关系与父列表中,主列并具有一个或多个读取从父列表上的字段的值,但取决于列表关系主要列第二列。SPRelatedField对象包含有关仅主要列中的信息。要获取有关第二列的信息,请调用SPFieldLookup对象,表示主要列的GetDependentLookupInternalNames方法。有关详细信息,请参阅GetDependentLookupInternalNames()方法。
示例
下面的示例是一个控制台应用程序,从客户列表中获取项中的网站集的其他列表中查找相关的项目。繁重是通过使用查阅列表中的项和从SPRelatedField对象的信息来构建针对相关的列表的查询的应用程序的PrintRelatedItems方法。
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