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.
Fortis Method
1 | public IEnumerable<INavigation> GetNavigation(IItemWrapper rootNode) |
Standard Sitecore Method
1 | public IEnumerable<INavigation> GetNavigation(IItemWrapper rootNode) |
The Problem
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
The solution is a custom AllTemplates computed field. This just iterates through all the base templates and builds the field.
1 | using System; |
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:
1 | <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> |
Coming up, more articles on how we use Fortis to streamline our Sitecore development and how we implemented a real time breaking news module using SignalR.
– Richard Seal