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:
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:
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).
The personalization is pretty straight forward, and reminds of the way you would set up rules in Microsoft Outlook:
Thanks to Emil Klein for the code.
READ MORE:
- Sitecore Digital Marketing System (DMS) Part 2: Creating Conditions to compare custom values by Oshyn
- Custom Conditions by Karbyn Interactive
- Ruling the DMS Rule Engine, a presentation from the Sitecore Symposium 2012
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/
LikeLike
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.
LikeLike
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.
LikeLike
Very helpful post! Thank you very much for posting!
LikeLike
Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog
Pingback: Why Sitecore Composable DXP is great news for developers and consultants | Brian Pedersen's Sitecore and .NET Blog
Pingback: Sitecore create custom Content Editor Warnings using Conditions and Rules | Brian Pedersen's Sitecore and .NET Blog