SPFieldLookup.CountRelated 属性

获取或设置一个布尔值,指定是否在查看当前列表项到查阅列表中显示的项的计数。

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

语法

声明
Public Overridable Property CountRelated As Boolean
    Get
    Set
用法
Dim instance As SPFieldLookup
Dim value As Boolean

value = instance.CountRelated

instance.CountRelated = value
public virtual bool CountRelated { get; set; }

属性值

类型:System.Boolean
true以返回到当前项 ; 与相关的项的数目否则为false。

备注

计数相关Lookup字段是Lookup字段执行反向查找并返回目标列表上的项的计数当前列表查找到某一项的变体。

当您将CountRelated属性设置为true时,应该配置当前的Lookup字段,以便指向目标列表上的另一个Lookup字段。此方法通过设置LookupList属性,以便它识别目标列表,并将LookupField属性,以便它在目标列表中指定的SPFieldLookup对象的内部名称。计数相关Lookup字段然后已计算的值等于与当前列表项相关目标列表中项的数目。

例如,假设您有两个列表,客户和订单。您希望谁下了订单,以便将客户 ID Lookup字段添加到订单列表并将其配置为指向客户列表中的 ID 字段显示订单列表上的项目。您还可以决定看一下有要能一眼看到多少每个客户的订单的客户列表。为了实现这一点,您向客户列表添加计数相关订单Lookup字段,并将其配置为指向订单列表上的客户 ID Lookup字段。然后,客户列表上的订单字段显示了放置每个客户的订单数。

备注

当您将CountRelated属性设置为true时, ReadOnlyFieldAllowDeletion属性将自动设置为true以及中。

示例

下面的示例是一个控制台应用程序,以处理两个列表,客户和订单。目标是使一个一眼看到多少个项该客户的客户列表中的项目的视图具有在订单列表中的用户。

应用程序开始,通过将两个列表的链接。这是通过在订单列表在列表中的客户 ID 字段指向创建客户 ID 查阅字段。然后应用程序在客户列表中创建一个订单字段指向订单列表中,在客户 ID 字段,将新字段的CountRelated属性设置为true。

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList customers = web.Lists.TryGetList("Customers");
                    SPList orders = web.Lists.TryGetList("Orders");

                    if (null != customers && null != orders)
                    {
                        SPField idField = customers.Fields.TryGetFieldByStaticName("ID");
                        if (null != idField)
                        {
                            // Create a Customer ID field on the Orders list. 
                            // Point it to the ID field on the Customers list.
                            string customerIdName = orders.Fields.AddLookup("Customer ID", customers.ID, true);
                            SPFieldLookup customerIdField = 
                                (SPFieldLookup)orders.Fields.GetFieldByInternalName(customerIdName);
                            customerIdField.LookupField = idField.InternalName;
                            customerIdField.Update();

                            // Add the field to the default view.
                            AddToDefaultView(orders, customerIdName);

                            // Create an Orders field on the Customers list.
                            // Point it to the Customer ID field on the Orders list.
                            string numOrdersName = customers.Fields.AddLookup("Orders", orders.ID, false);
                            SPFieldLookup numOrdersField = 
                                (SPFieldLookup)customers.Fields.GetFieldByInternalName(numOrdersName);
                            numOrdersField.LookupField = customerIdField.InternalName;
                            numOrdersField.CountRelated = true;
                            numOrdersField.Update();

                            // Add the field to the default view.
                            AddToDefaultView(customers, numOrdersName);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }

        static void AddToDefaultView(SPList list, string fieldName)
        {
            if (list != null && list.Fields.ContainsField(fieldName) && !list.DefaultView.ViewFields.Exists(fieldName))
            {
                SPView defaultView = list.DefaultView;
                defaultView.ViewFields.Add(fieldName);
                defaultView.Update();
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.OpenWeb()

                Dim customers As SPList = web.Lists.TryGetList("Customers")
                Dim orders As SPList = web.Lists.TryGetList("Orders")

                If customers IsNot Nothing AndAlso orders IsNot Nothing Then
                    Dim idField As SPField = customers.Fields.TryGetFieldByStaticName("ID")
                    If idField IsNot Nothing Then
                        ' Create a Customer ID field on the Orders list. 
                        ' Point it to the ID field on the Customers list.
                        Dim customerIdName As String = orders.Fields.AddLookup("Customer ID", customers.ID, True)
                        Dim customerIdField As SPFieldLookup = _
                           DirectCast(orders.Fields.GetFieldByInternalName(customerIdName), SPFieldLookup)
                        customerIdField.LookupField = idField.InternalName
                        customerIdField.Update()

                        ' Add the field to the default view.
                        AddToDefaultView(orders, customerIdName)

                        ' Create an Orders field on the Customers list.
                        ' Point it to the Customer ID field on the Orders list.
                        Dim numOrdersName As String = customers.Fields.AddLookup("Orders", orders.ID, False)
                        Dim numOrdersField As SPFieldLookup = _
                           DirectCast(customers.Fields.GetFieldByInternalName(numOrdersName), SPFieldLookup)
                        numOrdersField.LookupField = customerIdField.InternalName
                        numOrdersField.CountRelated = True
                        numOrdersField.Update()

                        ' Add the field to the default view.
                        AddToDefaultView(customers, numOrdersName)
                    End If
                End If
            End Using
        End Using
        Console.Write(vbLf & "Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    Sub AddToDefaultView(ByVal list As SPList, ByVal fieldName As String)
        If list IsNot Nothing AndAlso list.Fields.ContainsField(fieldName) _
            AndAlso Not list.DefaultView.ViewFields.Exists(fieldName) Then

            Dim defaultView As SPView = list.DefaultView
            defaultView.ViewFields.Add(fieldName)
            defaultView.Update()

        End If
    End Sub

End Module

另请参阅

引用

SPFieldLookup 类

SPFieldLookup 成员

Microsoft.SharePoint 命名空间