One of the really nice features in Fortis is the ability to be able to search based on template type. Because we create an interface for every template in Sitecore, we can use the full inheritance tree to bring out the data we need.
A good example of this would be when building navigation. At Lightmaker we have a standard Navigation template. This template gives us all the fields we need to build navigational elements. Our main Page Type template, inherits from the Navigation Template. We can use this to get a list of items in Sitecore.
public IEnumerable<INavigation> GetNavigation(IItemWrapper rootNode)
using (var searchContext = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
This works fine out of the box. But there is a problem when the template you want to filter is further down in the inheritance tree. The standard GetAllTemplates method used for the AllTemplates computed field, only uses the item.TemplateID and item.Template.BaseTemplates. Consider the scenario where you have 3 templates, Template A, Template B and Template C. Template B inherits from Template A and Template C inherits from Template B. If we want do a search for all items that have Template A in the inheritance tree, only items that directly use Template A or Template B will be returned. The expected result is that items that use ANY of the 3 templates should be returned.
The solution is a custom AllTemplates computed field. This just iterates through all the base templates and builds the field.
Notice that we are not using recursion here to build the list of templates. We could, but as this code gets called every time an item is indexed, recursion would be a bottle neck here. Instead we are using a stack object and iterating through the list. We could also make this code more robust and check for circular references, as that would cause an out of memory error eventually.
The final part of the puzzle is to setup the configuration, this would be the include file to use the new AllTemplates computed field: