So this might not be the most official solution for using the rules engine, but it just fitted in to the solution I’m currently working on. I got some elements that should be evaluated at runtime, whether they should be shown or not. The elements doesn’t have any presentation themselves, but they are used as a data source for a list view. It would might be possible to utilize conditional renderings, for this functionality, but I wanted to dig a little into the Rules engine, and this is the result.

Anyway, we got a number of brands; these are elements that contain a link and an image. Each element also contains a rule field that should determine whether the brand should be shown. This is resolved at runtime, depending on the currently logged in user.

I’ve implemented the template with a field of type DropLink, that points at a custom folder in /sitecore/system/settings/rules/custom/conditions, where I’ve created a number of conditions that are used to determine whether the user should have the brand shown or not.

The conditions are implemented as the following (this is an always true, but could be anything):

public class AlwaysTrueCondition<T> : WhenCondition<T> where T : RuleContext
        protected override bool Execute(T ruleContext)
            // custom logic to evaluate true or false, depending on the current user
return true; } }

Once we implemented four different rules that could apply on the current user, we need to be able to evaluate the rule selected for the element. I.e. in the tree shown above, the item "Energifyn” looks like to following:


The important field is the "ShowRule" field, when we evaluate the elements, we have data bound them to a list view, that renderers the image and a link. On the ItemDataBound event, we evaluate the field like this:

protected void lvBrands_OnItemDataBound(object sender, ListViewItemEventArgs e)
	Item brandItem = e.Item.DataItem as Item;
    if (EvaluateFirstRule(brandItem, "ShowRule"))
        // RENDER LINK

/// <summary>
/// Evaluates a rules field
/// </summary>
/// <param name="item">Item with field</param>
/// <param name="fieldName">fieldName of rule field</param>
/// <returns>true false depending on the rule evaluation</returns>
private bool EvaluateFirstRule(Item item, string fieldName)
    ReferenceField showRule = item.Fields[fieldName];
    if (showRule.TargetItem != null)
        // hacking the rules xml
        var rules = RuleFactory.ParseRules<RuleContext>(item.Database, 
            XElement.Parse("<ruleset><rule uid='" + 
            Sitecore.Data.ID.NewID + "'><conditions><condition id='" + 
            showRule.TargetID + "' uid='" + 
            Sitecore.Data.ID.NewID.ToShortID() + 
            "' /></conditions></rule></ruleset>"));
        var ruleContext = new RuleContext()
            Item = item
        if (rules.Rules.Any())
            return rules.Rules.First().Evaluate(ruleContext);

    return false;

The method EvaluateFirstRule, looks at the field, and returns true or false, depending on the rule given. The method is restricted to only one rule, as the field is a droplink, but could easily be extended for multiple rules.

This little example gave me some time to look into one of the corners of Sitecore that I hadn’t had time to look into before, the Rules Engine. In the project I am currently working on, we used the Rules Engine for two different features, more on this later.