SPFieldLookup.CountRelated property
取得或設定布林值,指定是否要在查閱清單,查看到目前的清單項目中顯示的項目計數。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
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; }
Property value
Type: System.Boolean
true傳回有關目前的項目 ; 項目數目否則, false。
備註
計數相關Lookup欄位是 variant 的Lookup欄位執行反向對應,並在 [目標] 清單上傳回的項目計數,查看項目到目前的清單。
當您將CountRelated屬性設定為true時,您應該設定目前的Lookup欄位,以便讓它指向另一個的Lookup欄位,在 [目標] 清單上。執行這項操作藉由設定LookupList屬性,以便用來識別目標] 清單中的色彩和LookupField屬性,使其在 [目標] 清單上指定的SPFieldLookup物件的內部名稱。計數相關Lookup欄位則會有計算的值等於目前的清單項目相關的 [目標] 清單中的項目數目。
例如,假設您有兩個客戶和訂單的清單。您想要顯示誰有編排順序,讓您將客戶 ID Lookup欄位新增到 [訂單] 清單,並將它設定為指向 [客戶] 清單中的 [ID] 欄位的 [訂單] 清單中的項目。您也可以決定當您查看您想要能夠查看多少訂單每位客戶的客戶清單有。要使這個變成可能的您將計數相關的訂單Lookup欄位新增到客戶清單,並將它設定為指向 [客戶編號Lookup欄位,在 [訂貨主檔] 清單上。接著客戶清單中的 [訂貨主檔] 欄位會顯示將置於每個客戶的訂單數量。
注意事項 |
---|
當您設定CountRelated屬性為true時, ReadOnlyField和AllowDeletion屬性會自動設定,以及true 。 |
Examples
下列範例是使用兩個清單,客戶和訂單的主控台應用程式。目標是讓使用者能夠在 [訂貨主檔] 清單檢視,查看多少項目的該客戶的客戶清單中的項目。
應用程式開始藉由連結兩個清單。它會藉由建立客戶 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