Indexing base templates – Sitecore 7 Content Search

Reason

Any large scale Sitecore solution worth its salt uses some degree of template inheritance to promote feature reuse and maintainability, whether it’s for the sake of actual field inheritance or as a tagging mechanism in the form of interface templates.
Base templates aren’t included in the Sitecore 7 Content Search indexes by default, although a matching field type is provided (Sitecore.ContentSearch.ComputedFields.AllTemplates). Luckily it’s very easy to add base templates to your index and use them in queries, as shown in the following example.

Examples are based on .NET 4.5 and Sitecore 7.1 rev. 130926.

Code

To make the base templates field available in the content indexes, save the configuration shown below to a .config-file (e.g. “z.BaseTemplates.config”) and place it in the “App_Config/Include”-folder.
It’s important to note that the configuration file has been prefixed with the letter “z”. This is done to ensure that it’s loaded after the configuration file “Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config”, which is also located in the “App_Config/Include”-folder. Our configuration won’t have any effect otherwise, as Sitecore loads config include files in lexicographical order (our configuration would be overridden by the default config).

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration>
        <defaultIndexConfiguration>
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="_basetemplates" storageType="yes" indexType="untokenized">Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch</field>
          </fields>
        </defaultIndexConfiguration>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

Shown below is a plain C# class utilizing property attributes from the Sitecore.ContentSearch framework. It uses the _basetemplates field along with the item name and the item’s ancestors (indexed as _name and _path by default).

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.Data;

[DebuggerDisplay("{ItemName}")]
public class BaseTemplateExample
{
  [IndexField("_basetemplates")]
  [TypeConverter(typeof(IndexFieldEnumerableConverter))]
  public IEnumerable<ID> ItemBaseTemplates { get; set; }

  [IndexField("_name")]
  public string ItemName { get; set; }

  [IndexField("_path")]
  [TypeConverter(typeof(IndexFieldEnumerableConverter))]
  public IEnumerable<ID> ItemAncestors { get; set; }
}

Writing a Linq-to-Sitecore query which uses the _basetemplates field is straight forward:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Sitecore.ContentSearch;
using Sitecore.Data;

// Exclude standard values and other non-content items.
Expression<Func<BaseTemplateExample, bool>> isContentItem =
  item => item.ItemAncestors.Contains(Sitecore.ItemIDs.ContentRoot);

// Exclude anything that doesn't inherit from the desired base template.
ID browserTitleTemplateID = new ID("{5C364328-D25A-48EE-81C9-BB6B9C335905}");
Expression<Func<BaseTemplateExample, bool>> hasBrowserTitleBaseTemplate =
  item => item.ItemBaseTemplates.Contains(browserTitleTemplateID);

ISearchIndex index = ContentSearchManager.GetIndex("sitecore_web_index");
using (IProviderSearchContext searchContext = index.CreateSearchContext())
{
  IList<BaseTemplateExample> matches = searchContext.GetQueryable()
                                                .Where(isContentItem)
                                                .Where(hasBrowserTitleBaseTemplate)
                                                .ToList();
}

Example

Base template content search example

One thought on “Indexing base templates – Sitecore 7 Content Search

  1. Pingback: Generic extension methods for Sitecore ContentSearch - Laub plus Co

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s