Custom rules and conditions for Sitecore personalization

In Sitecore it is possible to change the sublayouts or data sources on a page depending on conditions. This means that users can see the same page differently, depending on a set of rules. Sitecore comes standard with a set of standard rules (The user’s geographic place, has the user met any goals, has the user been referred etc.).

As always with Sitecore (well, except for the page editor) you can extend Sitecore’s functionality by creating your own rules.

THE EXAMPLE: QUERY STRING CONDITION

This is an example of a rule that checks for a certain QueryString (in this case a ZipCode). If the querystring contains a certain value, the condition is met.

First you need to set up the condition. Conditions are defined in folders below /sitecore/system/Settings/Rules/Segment Builder/Conditions:

My Custom Condition

My Custom Condition

The cryptic text attribute is used to explain and configure the condition at the same time:

Where the querystring parameter zipcode [operatorid,Operator,,compares to] [value,,,value]

The string is case sensitive. [operatorid,Operator,,compares to] gives you the options “equal to, not equal to, less than, greater than, …”, and [value,,,value] gives you the possibility to enter a value.

Then you need to write the code. This condition compares integers, so I inherit from the IntegerComparisonCondition. There are other conditions to inherit from as well, like the OrCondition, NotCondition, AndCondition, OperatorCondition, BinaryCondition etc.

namespace MyCode.Infrastructure
{
  public class CheckQuerystringCondition<T> : IntegerComparisonCondition<T> where T : RuleContext
  {
    protected override bool Execute(T ruleContext)
    {
      Assert.ArgumentNotNull(ruleContext, "ruleContext");
      var zipCodeQuerystringValue = HttpContext.Current.Request.QueryString["zipcode"];
      int zipCode;
      if (string.IsNullOrEmpty(zipCodeQuerystringValue))
      {
        return false;
      }

      if (!int.TryParse(zipCodeQuerystringValue out zipCode))
      {
        return false;
      }

      return Compare(zipCode);
    }
  }
}

The condition is now ready to be used.  To use it you need to find the layout of the page you wish to personalize. You can see if there is conditions on a layout by the little number mark:

Layout Details

Layout Details

Select the control to personalize and start personalizing. In this example I choose between 2 diferent sublayous depending on the condition. You can also choose to switch the data context (i.e. where the component gets its data from).

Personalize The Component

Personalize The Component

The personalization is pretty straight forward, and reminds of the way you would set up rules in Microsoft Outlook:

Rule Set Editor

Rule Set Editor

Thanks to Emil Klein for the code.

READ MORE:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, Sitecore 6, Sitecore 7 and tagged , , , , . Bookmark the permalink.

7 Responses to Custom rules and conditions for Sitecore personalization

  1. Jamie Stump says:

    Nice Post Brian, I have written a similar post that allows the content author to specify both the actual QueryString parameter as well as it’s value: http://blogs.perficient.com/portals/2012/12/14/sitecore-dms-in-action-querystringcondition/

    Like

  2. Priya says:

    Hi Brian,
    I have also implemented some custom rules in my site. I want to know where these rules are getting saved in database. Which database and table should i refer .
    If you have any idea, please do help me.

    Thanks & Regards,
    Priya.

    Like

  3. briancaos says:

    The rules themselves are stored in the MASTER/WEB database as Sitecore items.
    Once you have applied them to a layout, the rule configuration is stored in the Layouts field of that item.
    You should therefore not access the rules through any database connection, simply allow Sitecore to control the data for you.

    Like

  4. Very helpful post! Thank you very much for posting!

    Like

  5. Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog

  6. Pingback: Why Sitecore Composable DXP is great news for developers and consultants | Brian Pedersen's Sitecore and .NET Blog

  7. Pingback: Sitecore create custom Content Editor Warnings using Conditions and Rules | Brian Pedersen's Sitecore and .NET Blog

Leave a comment

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