SPRelatedField 类

提供有关从父列表中获取信息的子列表的查阅字段有关的信息。

继承层次结构

System.Object
  Microsoft.SharePoint.SPRelatedField

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

语法

声明
Public Class SPRelatedField
用法
Dim instance As SPRelatedField
public class SPRelatedField

备注

在Microsoft SharePoint Foundation两个列表成为相关的用户创建一个从另一个列表的字段中获取其值的列表上的查阅字段时。您可以将提供的数据与列表和列表来查找作为的数据。发现子列表中的哪些字段依赖于父列表中的信息,请对表示父列表的SPList对象调用GetRelatedFields()方法。

GetRelatedFields方法将返回具有以下属性的SPRelatedField对象的集合:

最后一个属性, RelationshipDeleteBehavior,代表列表关系的一个重要方面。创建查阅字段中的,通过用户界面或通过对象模型,用户可以设置从一个列表的删除操作如何影响从其他删除限制。例如,用户可以指定从父删除列表级联子级 ;即时从父列表中删除项目,还将删除子列表上的所有相关的项。用户还可以指定从父列表的删除操作是限制;如果子列表上存在相关的项不能从父列表中删除项目。有关详细信息,请参阅SPRelationshipDeleteBehavior枚举。

备注

为了限制删除,用户必须具有SPBasePermissions。ManageLists在父列表的权限。

在SharePoint Foundation用户还可以创建多列查阅。在这种情况下,子列表具有定义的关系与父列表中,列并具有一个或多个读取从父列表上的字段的值,但取决于列表关系主要列第二列。SPRelatedField对象包含有关仅主要列中的信息。要获取有关第二列的信息,请调用SPFieldLookup对象,表示主要列的GetDependentLookupInternalNames方法。

示例

以下示例是一个控制台应用程序,该应用程序报告向其他列表中的查阅字段提供数据的"客户"列表中字段的信息。对于表示主列的每个相关字段,该应用程序调用 GetDependentLookupInternalNames 方法来获取表示次列的字段列表。

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList targetList = site.Lists.TryGetList("Customers");
                    SPRelatedFieldCollection relatedFields = targetList.GetRelatedFields();

                    string strSeparator = new String('=', 70);
                    string strUnderline = new String('-', 20);
                    string strFormat = "Target Field: {0} | Related Field: {1}";

                    Console.WriteLine(strSeparator);
                    foreach (SPRelatedField fieldInfo in relatedFields)
                    {
                        using (SPWeb relatedSite = siteCollection.AllWebs[fieldInfo.WebId])
                        {
                            SPList relatedList = relatedSite.Lists.GetList(fieldInfo.ListId, false);
                            SPFieldLookup relatedField = relatedList.Fields[fieldInfo.FieldId] as SPFieldLookup;
                            SPField targetField = targetList.Fields.GetFieldByInternalName(relatedField.LookupField);

                            Console.WriteLine("\nTarget List: {0} ({1}) | Related List: {2} ({3})", 
                                targetList.Title, site.Title, relatedList.Title, relatedSite.Title);

                            Console.WriteLine("\nPrimary Column");
                            Console.WriteLine(strUnderline);
                            Console.WriteLine(strFormat, targetField.Title, relatedField.Title);
                            Console.WriteLine("Deletion behavior: {0}", relatedField.RelationshipDeleteBehavior);

                            Console.WriteLine("\nSecondary Columns");
                            Console.WriteLine(strUnderline);

                            List<string> dependents = relatedField.GetDependentLookupInternalNames();
                            for (int i = 0; i < dependents.Count; i++)
                            {
                                SPFieldLookup lookup = relatedList.Fields.GetFieldByInternalName(dependents[i]) as SPFieldLookup;
                                SPField field = targetList.Fields.GetFieldByInternalName(lookup.LookupField);

                                Console.WriteLine(strFormat, field.Title, lookup.Title);
                            }
                            Console.WriteLine();
                            Console.WriteLine(strSeparator);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using siteCollection As New SPSite("https://localhost")
            Using site As SPWeb = siteCollection.OpenWeb()
                Dim targetList As SPList = site.Lists.TryGetList("Customers")
                Dim relatedFields As SPRelatedFieldCollection = targetList.GetRelatedFields()

                Dim strSeparator As String = New [String]("="c, 70)
                Dim strUnderline As String = New [String]("-"c, 20)
                Dim strFormat As String = "Target Field: {0} | Related Field: {1}"

                Console.WriteLine(strSeparator)
                For Each fieldInfo As SPRelatedField In relatedFields
                    Using relatedSite As SPWeb = siteCollection.AllWebs(fieldInfo.WebId)
                        Dim relatedList As SPList = relatedSite.Lists.GetList(fieldInfo.ListId, False)
                        Dim relatedField As SPFieldLookup = TryCast(relatedList.Fields(fieldInfo.FieldId), SPFieldLookup)
                        Dim targetField As SPField = targetList.Fields.GetFieldByInternalName(relatedField.LookupField)

                        Console.WriteLine(vbLf & "Target List: {0} ({1}) | Related List: {2} ({3})", _
                                          targetList.Title, site.Title, relatedList.Title, relatedSite.Title)

                        Console.WriteLine(vbLf & "Primary Column")
                        Console.WriteLine(strUnderline)
                        Console.WriteLine(strFormat, targetField.Title, relatedField.Title)
                        Console.WriteLine("Deletion behavior: {0}", relatedField.RelationshipDeleteBehavior)

                        Console.WriteLine(vbLf & "Secondary Columns")
                        Console.WriteLine(strUnderline)

                        Dim dependents As List(Of String) = relatedField.GetDependentLookupInternalNames()
                        Dim i As Integer = 0
                        While i < dependents.Count
                            Dim lookup As SPFieldLookup = _
                                TryCast(relatedList.Fields.GetFieldByInternalName(dependents(i)), SPFieldLookup)
                            Dim field As SPField = targetList.Fields.GetFieldByInternalName(lookup.LookupField)

                            Console.WriteLine(strFormat, field.Title, lookup.Title)
                            i = i + 1
                        End While
                        Console.WriteLine()
                        Console.WriteLine(strSeparator)
                    End Using
                Next
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module

该控制台应用程序打印类似于以下示例的输出。

======================================================================

Target List: Customers (Team Site) | Related List: Complete Orders (Team Site)

Primary Column
--------------------
Target Field: ID | Related Field: Customer ID
Deletion behavior: None

Secondary Columns
--------------------
Target Field: Last Name | Related Field: Customer

======================================================================

Target List: Customers (Team Site) | Related List: Addresses (Team Site)

Primary Column
--------------------
Target Field: ID | Related Field: Customer ID
Deletion behavior: Cascade

Secondary Columns
--------------------
Target Field: Last Name | Related Field: Last Name
Target Field: First Name | Related Field: First Name

======================================================================

Target List: Customers (Team Site) | Related List: Pending Orders (Team Site)

Primary Column
--------------------
Target Field: ID | Related Field: Customer ID
Deletion behavior: Restrict

Secondary Columns
--------------------
Target Field: First Name | Related Field: First Name
Target Field: Last Name | Related Field: Last Name
Target Field: Phone | Related Field: Phone

======================================================================

Press ENTER to continue...

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPRelatedField 成员

Microsoft.SharePoint 命名空间

GetRelatedFields()

GetDependentLookupInternalNames()

SPRelatedFieldCollection

SPFieldLookup

IsRelationship

IsDependentLookup

SPRelationshipDeleteBehavior