Insert Rules using Rules Magic

Who hasn’t run into something before that requires a new Folder type because it needs specific Insert rules, and have that folder type excluded in specific renderings because it shouldn’t be displayed?

Turns out, we don’t need different folder types anymore, Sitecore provides a very easy solution for this – the Rules engine.

Let’s say we have a News folder which can insert either a News item or a folder for Categories, the Categories folder can insert either a News item or a folder for Subcategories and the Subcategories can only insert News items. Sure, it’s not the most nicely or logically structured tree, but I needed an example ;-).

news structure

It goes without saying that we won’t be able to set the insert rules on the Folder template, as we need different insert options for different locations in the tree.

We can already set the News insert options manually if we’d like, because there won’t be a second news folder. The Category folder needs to be able to insert Folders (which is the default insert option for a Folder anyway) and a News item. We can’t go and set the insert options manually here, because there might be new categories later on. The same goes for the Subcategory.

There is already an Action defined for this, and there are some out-of-the-box Rules which suit our purpose in this case.,
If we do need to have our own, Sitecore is extensible enough for us to be able to customize Actions and Rules.

In /sitecore/System/Settings/Rules/Insert Options/Rules we can insert our own new rule.

As the Rule, we can use a Sitecore default ‘where the item is the specific item or one of its subitems’. The same goes for the action – we can use ‘add specific insert option’.

All we need to do is set the specific item to be News, and specific insert option to be our News article.

generic rule

So now we’ll have the insert option Folder and News article for News, Category and Subcategory. Of course, we don’t want the Folder insert option on our Subcategory. What can we do about this?

Well, if there’s an ‘add specific insert option’, I feel like there should also be a ‘remove specific insert option’. This rule doesn’t exist yet, so we’ll create one.

First of all, add the new Action to the /sitecore/System/Settings/Rules/Insert Options folder.

We can copy and paste the value for the Text field from the ‘add specific insert option’ rule – we’ll just change ‘add’ to ‘remove’.


In the Type field I’ll point to my class, which we’ll have to create first. Most of the logic already exists in Sitecore’s AddInsertOption class, so that’s a perfect starting point. In fact, it’s so perfect, we only have to change one line (well, 4 really) – it’s highlighted below.

namespace TestApplication.InsertOptions
  public class RemoveInsertOption<T> : RuleAction<T> where T:InsertOptionsRuleContext
    // Fields
    private ID insertOptionId;

    // Methods
    public override void Apply(T ruleContext)
      Assert.ArgumentNotNull(ruleContext, "ruleContext");
      var item = ruleContext.Item;
      if (item != null)
        var item2 = item.Database.GetItem(this.InsertOptionId);
        if (item2 != null)
          ruleContext.InsertOptions.RemoveAll(delegate(Item i)
                                       return i.ID == item2.ID;

    // Properties
    public ID InsertOptionId
        return (this.insertOptionId ?? ID.Null);
        Assert.ArgumentNotNull(value, "value");
        this.insertOptionId = value;

The InsertOptionId is set through the Text field of our Rule item.

The apply method works as follows:

ruleContext.Item checks the item the rule is run on. Item2 is then the specific template to remove. I use RemoveAll because of the off-chance it appears twice.

After building this we can add the Type field to point to our new class:


We can now create a new Rule as well. We need our rule to remove the insert rule when the item is under the News folder and the depth is 5 levels.


And our result after all this work.








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

This site uses Akismet to reduce spam. Learn how your comment data is processed.