Programmatically Get Personalization Datasource Items of a Sitecore Item

In the previous blog post, Programmatically Get Datasource Items of a Sitecore Item we learned how to get the datasource items from the rendering references. This does not include the items that have been added through personalization and rules. By adding the following methods to our previous post, we can get those items.

        public static List<Item> GetPersonalizationDataSourceItems(this Item i)
        {
            List<Item> list = new List<Item>();
            foreach (RenderingReference reference in i.GetRenderingReferences())
            {
                list.AddRange(reference.GetPersonalizationDataSourceItem());
            }
            return list;
        }

        private static List<Item> GetPersonalizationDataSourceItem(this RenderingReference reference)
        {
            List<Item> list = new List<Item>();
            if (reference != null && reference.Settings.Rules != null && reference.Settings.Rules.Count > 0)
            {
                foreach (var r in reference.Settings.Rules.Rules)
                {
                    foreach (var a in r.Actions)
                    {
                        var setDataSourceAction = a as Sitecore.Rules.ConditionalRenderings.SetDataSourceAction<Sitecore.Rules.ConditionalRenderings.ConditionalRenderingsRuleContext>;
                        if (setDataSourceAction != null)
                        {
                            Item dataSourceItem = GetDataSourceItem(setDataSourceAction.DataSource, reference.Database);
                            if (dataSourceItem != null)
                            {
                                list.Add(dataSourceItem);
                            }
                        }
                    }
                }
            }
            return list;
        }

This code loops through the rules of your personalized component and if the action is Sitecore.Rules.ConditionalRenderings.SetDataSourceAction, then we know that this will be one of the personalization items. Next we can write some nice code like this:

        foreach (Item personalizationItem in Sitecore.Context.Item.GetPersonalizationDataSourceItems())
        {
             // do something
        }

Please see the next blog post in this series: Programmatically Get Multi Variate Test Datasource Items of a Sitecore Item

Advertisements

6 Responses to “Programmatically Get Personalization Datasource Items of a Sitecore Item”

  1. Programmatically Get Multi Variate Test Datasource Items of a Sitecore Item | Horizontal Integration Says:

    […] Programmatically Get Personalization Datasource Items of a Sitecore Item […]

  2. Programmatically Get Datasource Items of a Sitecore Item | Horizontal Integration Says:

    […] Programmatically Get Personalization Datasource Items of a Sitecore Item […]

  3. Datasource Workflow Module | Horizontal Integration Says:

    […] Programmatically Get Personalization Datasource Items of a Sitecore Item […]

  4. Federico Says:

    Hi, great and useful post! I still need one more thing and I dont know how to do it. Do you know if there is a way to get only the rules that are active/valid?? For example, today is Monday and I would like to get the dataSource from the rules that ask if today is Monday. I have checked Rules parameters but cant find a solution. Thank you in advanced!

    • Brent Svac Says:

      You can open up this class: Sitecore.Mvc.Analytics.Pipelines.Response.CustomizeRendering.Personalize in your favorite decompiler (I use dotPeek or Just Decompile) and see how Sitecore is processing the rules. Basically I think you would do something like this:
      foreach (var r in reference.Settings.Rules.Rules)
      {
      r.Condition.Evaluate(ruleContext, stack)
      }

      You can check that class mentioned above to see how to get the rule context and stack. After you run the conditions, you’ll have to check either the context or stack to see which rules passed.

      • Vivek Says:

        Hi Brent,
        Thank you for this wonderful post. It guided me so fix one of my issue. The issue in some extent was like Federico’s . What i wanted was a first personalized data source (which satisfy some condition) from the list of personalized rule list defined under presentation > details.
        The code which does this :
        ConditionalRenderingsRuleContext context = new ConditionalRenderingsRuleContext(rderings, reference);
        reference.Settings.Rules.RunFirstMatching(context);
        datasource= reference.Settings.DataSource;
        where rderings is a collection that i get from the method i.GetRenderingReferences()

        Cheers,


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: