Indexing base templates – Sitecore 7 Content Search


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.


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="">
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="_basetemplates" storageType="yes" indexType="untokenized">Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch</field>

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;

public class BaseTemplateExample
  public IEnumerable<ID> ItemBaseTemplates { get; set; }

  public string ItemName { get; set; }

  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()


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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s