Personalizing Staticlly Based Componenets with the Sitecore Rules Engine


Someone in the SDN forums had asked how to personalize a statically placed component on a page so I thought I’d come up with a solution. One one option would of course be to use template inheritance and to personalize the component on your base page template but that’s no longer a statically placed component.

I decided to create a web control that has settings for the default rendering , datasource, and parameters but allows you to personalize the component via the rules engine.  My first attempt had me creating my own rule context and actions but after a quick rewrite I realized I didn’t need all of that and was able to just use the per-existing context and actions.

Here’s the solution.

Create a Sitecore Template with a Field and call it “Rendering Rule” and make it of type Rule and set the Datasource to “/sitecore/system/Settings/Rules/Conditional Renderings”.

Add a new one to the tree and set the rendering rule. Something easy to test is “Where the current user is adminstrator change the rendering item”. Just make sure not to try and test in preview.

Next, I created the control below which will execute the rule actions for the item

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Sitecore.Rules.ConditionalRenderings;
using Sitecore.Data.Items;
using Sitecore.Web.UI.WebControls;
using System.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Data.Fields;
using Sitecore.Layouts;

namespace ConditionalRenderings
{
    public class StaticRendering : WebControl
    {
        public string DataSource { get; set; }
        public string RenderingItem { get; set; }
        public bool IsHidden { get; set; }
        public string RenderingRule { get; set; }
        public string Parameters { get; set; }

        protected override void OnLoad(EventArgs e)
        {

            Assert.IsNotNull(RenderingRule, "Rendering Rule cannot be null");
            Item RenderingRuleItem = Sitecore.Context.Database.SelectSingleItem(RenderingRule);
            Assert.IsNotNull(RenderingRuleItem, "Rendering Rule Item not found.");
            Assert.IsNotNull(RenderingItem, " Rendering Item cannot be null");
            Item renderingItem = Sitecore.Context.Database.SelectSingleItem(RenderingItem);

            //create the rendering reference and set some defaults
            RenderingReference reference = new RenderingReference(new RenderingItem(renderingItem));
            Assert.IsNotNull(RenderingItem, "Rendering Item not found");

            if (!string.IsNullOrEmpty(DataSource))
            {
                reference.Settings.DataSource = DataSource;
            }

            if (!string.IsNullOrEmpty(Parameters))
            {
                reference.Settings.Parameters = Parameters;
            }

            List references = new List();
            references.Add(reference);

            //new up the rule context and execute the rule
            ConditionalRenderingsRuleContext ruleContext = new ConditionalRenderingsRuleContext(references,reference);

            RuleList rules = RuleFactory.ParseRules(Sitecore.Context.Database, RenderingRuleItem.Fields["Rendering Rule"].GetValue(true));
            rules.Run(ruleContext);

            //make sure hide component wasn't selected as an action
            if (ruleContext.References.Count >= 1)
            {
                //add the control to the page
                this.Controls.Add(ruleContext.Reference.GetControl());
            }
            base.OnLoad(e);
        }

    }
}

Next, Add the control source to the web config under system.web/pages/controls.

<add tagPrefix=”cr” namespace=”ConditionalRenderings” assembly=”ConditionalRenderings”/>

Finally, add the control to a page pointing it at the Rendereing Rule Item we created and and default Rendering Item.

<cr:StaticRendering runat=”server” RenderingItem=”{2B556B84-364A-4AB3-A64C-933787D19619}” RenderingRule=”{11BD1B9F-A953-4F80-8692-4663B6DB4C78}” />

Package to just download and install is available here. You will still have to update the web.config to use the control.

Advertisements