Sitecore Rules Engine: How to create a custom condition

In this How to I will build a custom condition for the Sitecore Rules Enginge. This How To is a proof of concepts based on a webinar (in Dutch)  about the Rules Engine. I presented this webinar earlier this year and you can watch the webinar on Youtube by clicking this link.

Before we start writing code I’ll explain the business case we are trying to resolve.

The ‘case’:
The business user want to personalize the website based on the website the visitor comes from (the http referrer).  For now he is only interested in Facebook and Twitter.
If the visitor visits our site from Facebook the Facebook Like button of our own Facebook page must be visible on the homepage.
If the visitor visits our site from Twitter our Twitter Tweet stream must be visible on the homepage.

Oke, Now start building it!

Create templates

Create a template called Sidebar Text, this template has two field; a Sidebar Title (Single-Line Text field) and a Sidebar Text (Rich Text field).

Create a template called Standaard Text, this template one field called Text (Multi-line Text).

Set up the content tree

Create a folder called Sidebars  and add two Sidebar Text items with the following values:

Create a folder called Http Referrers and add the following items:

  • Item name: Twitter
    • Text:
  • Item name: Facebook
    • Text:

Create presentation component

Create the Sidebar Text rendering for displaying the Sidebar Title and Sidebar Text.

Open development center and click Create a New XSLT rendering and name the rendering Sidebar Text. Add the following code to the rendering template:

[code language=”html”]


Create the HttpReferrerEquals custom condition

To change the datasource of a item based on the http referrer we need to create a custom condition that will check if the http referrer equals a Facebook,Twitter or any other specified.

Create the custom condition by creating a new class HttpReferrerEquals and inherit from the WhenCondition class. In the HttpReferrerEquals class create a public property HpptReferrerItemId, later this article we will map this public property with the Sitecore Rules Engine. Override the Execute method and create the condition logic.

[code language=”C”]
public class HttpReferrerEquals : WhenCondition where T:RuleContext
//value of property set by Sitecore
public string HttpReferrerItemId { get; set; }

protected override bool  Execute(T ruleContext)
if (!string.IsNullOrEmpty(HttpReferrerItemId))
Item referrer = Sitecore.Context.Database.GetItem(HttpReferrerItemId);

//Check if referrer equals Referrer item.
if (referrer != null &&
HttpContext.Current.Request.UrlReferrer != null &&
return true;
return false;

For this class to work you need to add the following references

[code language=”C”]
using Sitecore.Data.Items;
using Sitecore.Rules.Conditions;
using Sitecore.Rules;

Compile your solution/project and add the assembly to the /bin folder of your Sitecore instance.

Add the If Http Referrer Equals condition to sitecore

Login to the Sitecore Desktop and open the Content Editor. In the Content Editor go to /sitecore/system/Settings/Rules/Conditional Renderings/Conditions and create a new Condition. Name the condition If Http Referrer Equals.

In the Text field add the following text:
where the http referrer is [HttpReferrerItemId,tree,root=/Sitecore/Content/Meta-Data/Http Referrers/,http referrer]

The [HttpReferrerItemId,tree, root=/Sitecore/Content/Meta-Data/Http Referrers/,http referrer] has four parameters that are comma separated.

[Param 1, Param 2, Param 3, Param 4]

Parameter 1:  The name of the property that the parameter sets in the .NET class that implements the condition. In this example the public property HttpReferrerItemId.

Parameter 2:  The name of the Macro item. You can find the macro items beneath /Sitecore/System/Settings/Rules/Common/Macros. In this example we use “Tree”, this macro invokes the Select an Item dialog.

Parameter 3:  The URL parameters you want to apply on the Macro chosen at Parameter 2. In this example we supply the root of the selection tree.

Parameter 4: The text that is display if the user has not specified a value for the parameter.

In the Type field add your class and assembly.
Sitecore.Starterkit.CustomCode.HttpReferrerEquals, Sitecore.Starterkit

Create the conditional rendering rules

Now we are going to create a Conditional Rendering rule based on the previous create custom condition. This conditional rendering is going to change the datasource based on the referrer. We will create a rule for each referrer.

Open the marketing center and go to Personalization, Rules. In the Rules section create a folder with the name Http Referrer Rules. Within the Http Referrer Rules folder create a new Conditional Rendering Rule. Name the Conditional Rendering Rule If visitor comes from Twitter and edit the rule by clicking on Edit Rule. In the Rule Set Editor dialog set the condition to ‘where the http referrer is http referrer’ and select the action ‘set data source to Item´. In the Rule description (step 3) click http referrer and select Twitter, click Item and select the /Sidebars/Twitter item.

Repeat these steps to create a conditional rendering rule for Facebook. The rule should look like this:

Apply the Conditional Rendering rules to the presentation component.

Add the Sidebar Text rendering to a placeholder on your website and open the placeholder properties. Set the Datasource property to the /Sidebars/Default Message item.

Find the Personalization section in the Sidebar Text control properties. Select the If visitor comes from Facebook rule and the If visitor comes from Twitter rule.

Publish the website.

Test the Http Referrer Rules

The best way to test if the rules are working is to spoof the http referrer in the browser.You can arrange this with the refspoof plugin for Firefox. Restart Firefox and verify that refspoof is installed. If you have problems with Firefox plugin compatibility check you can read the article: Firefox disable plugin compatibility check

Go to the website. The website I used for creating this is the Sitecore Training website. When I visit the website, without a http referrer it will look like this.

Now insert into refspoof and press enter, the Facebook widget will show.

And if I insert into refspoof, the site shows the Twitter widget.

You can watch the webinar on youtube, the webinar is in Dutch you can see the results at 25:30.


And that’s the end of this blog post. If you have any questions please leave a comment.

4 Replies to “Sitecore Rules Engine: How to create a custom condition”

Leave a Reply

Your email address will not be published. Required fields are marked *