Inherits from template condition for rules engine

Reading time ~1 minute

After Helix appeared, we pay more attention while building our solutions. To fulfill template inheritance principle I created additional inherits from condition for the rules engine.

Introduction

A commonly made mistake is checking before enabling or executing something in Sitecore whether a current item’s template has a specific ID or Name.

While building solutions with a Helix principles we should take into account that some of our modules might be extended by other developers. So we should pay more attention whenever we are making any decisions based on a template.

Checking just anID or Name is not enough now. That’s why I implemented a small yet very useful condition for rules engine to make things more extensible.

Implementation

Create code

Create new class and name it ItemTemplateCondition.cs.

Fill it with the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace Sitecore.Playground.Rules.Conditions
{
    public class ItemTemplateCondition<T> : WhenCondition<T> where T : RuleContext
    {
        public ID TemplateId { get; set; }

        protected override bool Execute(T ruleContext)
        {
            Assert.ArgumentNotNull(ruleContext, "ruleContext");
            Item item = ruleContext.Item;
            if (item == null || TemplateId == ID.Null)
            {
                return false;
            }

            return TemplateManager
                .GetTemplate(item.TemplateID, item.Database)
                .InheritsFrom(TemplateId);
        }
    }
}

Nothing hard can be found here. We are just returning condition result based on the knowledge whether a template of a current item inherits from specific template.

Create condition item

  • Create new item using /sitecore/templates/System/Rules/Condition template.

  • Set field values like on the picture below

Text field value:

where the item template inherits from [templateid,Tree,root={3C1715FE-6A13-4FCF-845F-DE308BA9741D},specific] template

Type field value:

Sitecore.Playground.Rules.Conditions.ItemTemplateCondition,Sitecore.Playground

Of course, you can change the namespaces in code and item if you want. I made it like that because I prefer using real namespaces rather than tokens.

Usage

Now you can use that condition inside your Rules fields.

Summary

I am using this condition for my PowerShell scripts (for filtering Insert Item integration point options), so I created my condition item here:

/sitecore/system/Settings/Rules/Definitions/Elements/PowerShell/

If you need to use this condition somewhere else make sure you define a path to it in Source field of your Rules field definition.

Below you can see how it is done in Sitecore Powershell Extensions

If you have any question feel free to ask.

Sitecore Extensions for Mozilla Firefox (self-hosted)

The new distribution method for Firefox addon. Continue reading

Sitecore Extensions version 3.2 released

Published on June 26, 2017

SXA Extensions - Multisite Management

Published on May 15, 2017