all things Sitecore related

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).

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Sidebar Template

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Standard Text Template

Set up the content tree

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Sidebars folder

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

  • Item name: Twitter
    • Text: twitter.com
  • Item name: Facebook
    • Text: facebook.com

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge http referrer folder

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:



<p><sc:text field="sidebar title"/> </p>

<p><sc:text field="sidebar text"/> </p>


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.


public class HttpReferrerEquals<T> : WhenCondition<T> 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 &amp;amp;amp;amp;&amp;amp;amp;amp;
HttpContext.Current.Request.UrlReferrer != null &amp;amp;amp;amp;&amp;amp;amp;amp;
HttpContext.Current.Request.UrlReferrer.ToString().ToLower().Contains(referrer["Text"].ToLower()))
{
return true;
}
}
return false;
}
}

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


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.

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Rule Set Editor

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Custom Facebook rule

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge refspoof

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.

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Result normal 300x213

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Result facebook 300x211

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

Sitecore Rules Engine: How to create a custom condition Sitecore Rules Enginge Result Twitter 300x209

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

[youtube]UWYTtDyM3WM[/youtube]

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

Series NavigationSitecore Rules Engine: How to remove conditions and actions >>

Tags: , , , , , ,

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

There Are 3 Brilliant Comments

Trackback URL | Comments RSS Feed

  1. Sheetal Jain says:

    Very Informative article and very helpful

Post a Comment

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

Top