4

Context Item- mypage
-- Presentation
----Controller Rendering -R1 with datasource value -D1
-----R1- Personalize Content.D2

How do I refer both these data content and get it rendered in given page context?

Let me know if queries or require more explanation.

3
  • You want both the personalized datasource (A/B testing) and the original datasource?
    – Chris Auer
    Commented Nov 25, 2016 at 2:19
  • Yes i need both Commented Nov 25, 2016 at 2:44
  • What is it exactly that you want to be rendered? Commented Nov 28, 2016 at 14:33

1 Answer 1

3

By the time the below code is accessed in Controller/View Rendering, any personlization rules are applied.

So, the DataSource and Item properties point to the the datasource are the personalized ones configured for the rule that is applied.

Sitecore.Mvc.Presentation.RenderingContext.Current.Rendering.DataSource
Sitecore.Mvc.Presentation.RenderingContext.Current.Rendering.Item  

Below code shows how to get the RuleList xml and pull the default DataSource out of it.

string text = rendering.Properties["RenderingXml"];
  if (string.IsNullOrEmpty(text))
   {
     var renderingReference =  
     new RenderingReference(XmlExtensions.ToXmlNode(XElement.Parse(text)), <contextlanguage>, <contextdatabase>);

     var DefaultDataSource = renderingreference.Settings.DataSource;
   }

Rendering xml with personalization rules

Once the Rendering reference is obtained, the default datasource can be pulled from its Settings.DataSource property.

enter image description here

The order of DataSource for the rendering will be

  1. DataSource selected in the Action section of Rule that is applied
  2. The order of Default DataSource will be
    • DS selected in the Action section
    • If Action Section is empty, then DS selected from Rendering Parameters
    • If none of them are set then RenderingReference.Settings.DataSource is empty. But the actual Rendering DataSource set to PageItem
4
  • Could you please clarify in your answer: 1) How to get the original data source? 2) How to get the personalized data source? Commented Nov 28, 2016 at 14:32
  • @DmytroShevchenko updated my answer. 1) By default the datasource provided to the Rendering is Personalized datasource based on Rule applied. 2) I added code section to pull the Default DataSource from Rules XML for reference.
    – phani
    Commented Nov 28, 2016 at 17:28
  • @Santosh You can inherit the Sitecore.Mvc.Analytics.Pipelines.Response.CustomizeRendering.Personalize, Sitecore.Mvc.Analytics class and override the RunRules method to attach an event when the rule is applied rules.Applied+= new RuleActionEventHandler<ConditionalRenderingsRuleContext>(<yourmethod>). When the rules are processed this event will get executed. You can store the default datasource during this time into the Rendering parameters and use it later in your View/Controller.
    – phani
    Commented Nov 30, 2016 at 18:01
  • Thanks Phani, this is what i m looking for. I'll try this. Commented Dec 5, 2016 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.