LibrarySites.Banner

Sitecore 7: Custom Classes as Data Template Field Sources

This blog post explains a technique that you can specify classes that contain logic to determine the source items to include in fields that present lists when working in the Sitecore ASP.NET Web Content Management System (CMS) and Customer Engagement Platform (CEP).

Certain types of fields such as multilists allow the user to select one or more items from a flat list. To determine the items to include in such lists, Sitecore invokes the getLookupSourceItems pipeline. Processors in this pipeline determine whether to return a list by checking the Source property of the field definition item, which may indicate a Sitecore query or item.

In Sitecore 7, the /App_Config/Include/Sitecore.Buckets.config Web.config include file adds the CustomDataSources processor to this pipeline. If the Source property of the data template field does not begin with the code: prefix, the CustomDataSources pipeline does nothing. Sitecore invokes the ProcessQuerySource processor. If the Source property does not specify a Sitecore query, the ProcessDefaultSource processor determines an item from the Source property and returns its children. If the Source property does not specify an item, then the ProcessDefaultSource processor returns the children of the item selected in the Content Editor.

If the Source property of the data template field begins with the code: prefix, then the CustomDataSources processor creates an instance of the class specified by that Source property, which implements the Sitecore.Buckets.FieldTypes.IDataSource interface. The CustomDataSources processor invokes the ListQuery() method defined by the IDataSource interface to determine the items to include in the list.

 

To implement a class that you can use with this technique to determine the items to include in a list:

  1. In a Visual Studio project, create a class that implements the Sitecore.Buckets.FieldTypes.IDataSource interface. Your project must reference the Sitecore.Buckets.dll assembly. I recommend that you set the Copy Local property of that reference to false.
  2. In that class, implement the ListQuery() method to determine the items to include in the list. The following arbitrary example returns the children of the item selected in the Content Editor unless that item is a direct child of the /sitecore/content item, in which case it returns the children of the /sitecore/content item instead of the selected item.
    namespace Sitecore.Sharedsource.Data.FieldSources
    {
      using SC = Sitecore;
      
      public class CustomFieldDataSource : SC.Buckets.FieldTypes.IDataSource
      {
        public SC.Data.Items.Item[] ListQuery(SC.Data.Items.Item item)
        {
          if (item.Parent.ID == SC.ItemIDs.ContentRoot)
          {
            return item.Parent.Children.ToArray();
          }
      
          return item.Children.ToArray();
        }
      }
    }
  3. Specify the class in the Source property of the data template field definition item using the code: prefix.
    code:Sitecore.Sharedsource.Data.FieldSources.CustomFieldDataSource,Sitecore.Sharedsource

Alternatively, you could add a processor to the getLookupSourceItems pipeline.

Resources