João Neto's Blog

João Neto's Blog

How to evaluate a Condition Rendering Rule from the code

How to evaluate a Condition Rendering Rule from the code

Subscribe to my newsletter and never miss my upcoming articles

In this post, I'll be covering how to list the Personalization Rules on a given page from a Rendering Controller. In order achieve it, I'll be talking about how to get only the renderings that contain personalization rules and how to evaluate the personalization rule as well.

Recently, I was trying to find a way to list the Personalization Rules on a given page from a Rendering Controller and evaluate which one is currently in action.

I came across to this nice post in the Sitecore Community How to invoke a Condition Rendering Rule from Code , but it did not work for me due to the RuleContext class.

Item item = Sitecore.Context.Item;

Sitecore.Data.Database contextDB = Sitecore.Configuration.Factory.GetDatabase(item.Database.Name);

Sitecore.Data.Items.Item ruleItem = contextDB.GetItem(new Sitecore.Data.ID(SitecoreConstants.ItemGUID), item.Language);

String rule = ruleItem.Fields["Rule"].Value;

var rules = RuleFactory.ParseRules<RuleContext>(item.Database,XElement.Parse(rule));

var ruleContext = new RuleContext() { Item = item };

if (rules.Rules.Any())
  return rules.Rules.First().Evaluate(ruleContext);

It turns out that when you are evaluating Condition Rendering Rule inside a Rendering Controller you need to use ConditionalRenderingsRuleContext instead.

With the help of Diego Moretto in the StackExchange post How can I programmatically list all personalization in my page? I was able to write the code below:

public ActionResult GetPersonalizationRule()
{
    var item = Sitecore.Context.Item;

    Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__renderings"];
    Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(Sitecore.Context.Device);

    var renderingsWithPersonalization = renderings.Where(r => r.Settings.Rules.Count > 0).ToList();

    List rulesName = new List();

    foreach (var rendering in renderingsWithPersonalization)
        foreach (var rule in rendering.Settings.Rules.Rules)
        {
            var ruleContext = new Sitecore.Rules.ConditionalRenderings.ConditionalRenderingsRuleContext(renderings.ToList(), rendering);
            if (rule.Evaluate(ruleContext)) rulesName.Add(rule.Name);
        }

    return View(rulesName);
}

In the code above, we are getting only the renderings that contain personalization rules .Where(r => r.Settings.Rules.Count > 0).

image.png

In Sitecore, you set the personalization rule in the rendering as shown in the image below:

image.png

Next, we evaluate the Personalization Rule by creating a ConditionalRenderingsRuleContext with the renderings in the page and the rendering that contains the personalization rule, and by invoking the Evaluate method.

image.png

The end result is that I list the name of the Personalization Rules that are currently being displayed in my page:

image.png

 
Share this
Proudly part of