Share via


SPFieldLookup.CountRelated property

Gets or sets a Boolean value that specifies whether to display the count of items in the lookup list that look up to the current list item.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

public virtual bool CountRelated { get; set; }

Property value

Type: System.Boolean
true to return the number of items related to the current item; otherwise, false.

Remarks

Count-related Lookup fields are a variant of Lookup fields that perform a reverse lookup and return the count of items on the target list that look up to an item on the current list.

When you set the CountRelated property to true, you should configure the current Lookup field so that it points to another Lookup field on the target list. Do this by setting the LookupList property so that it identifies the target list and the LookupField property so that it specifies the internal name of an SPFieldLookup object on the target list. The count-related Lookup field then has a computed value equal to the number of items in the target list that are related to the current list item.

For example, suppose you have two lists, Customers and Orders. You want items on the Orders list to show who has placed an order, so you add a Customer ID Lookup field to the Orders list and configure it to point to the ID field on the Customers list. You also decide that when you look at the Customers list you want to be able to see at a glance how many orders each customer has. To make this possible, you add a count-related Orders Lookup field to the Customers list and configure it to point to the Customer ID Lookup field on the Orders list. The Orders field on the Customers list then displays the number of orders that each customer has placed.

Note

When you set the CountRelated property to true, the ReadOnlyField and AllowDeletion properties are automatically set to true as well.

Examples

The following example is a console application that works with two lists, Customers and Orders. The goal is to enable a user who views an item in the Customers list to see at a glance how many items that customer has in the Orders list.

The application begins by linking the two lists. It does this by creating a Customer ID lookup field in the Orders list that points to the ID field in the Customers list. Then the application creates an Orders field in the Customers list, points it to the Customer ID field in the Orders list, and sets the new field's CountRelated property to 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();
            }
        }
    }
}

See also

Reference

SPFieldLookup class

SPFieldLookup members

Microsoft.SharePoint namespace