How to evaluate a Condition Rendering Rule from the code

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.

How to Invoke Condition Rendering Rule from Code code
Image 1: Code from Sitecore Community post

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

getting the personalization from the renderings field
Image 2: Getting only the renderings that contain personalization rules

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

Rendering Personalization
Image 3: Seeing Personalization Rules

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.

evaluate the personalization rule
Image 4: Evaluating Personalization Rules

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

List personalization rule end result
Image 5: Listing Personalization Rules

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s