0

I have built a custom peronsalization rule that sets a theme against a component. Each user can also choose a site theme which is stored against a contact.

I then want to use this logic to build up themed search results. So person in theme A will see search results A and so on.

Currently in the crawler I get all renderings I then parse the renderings rule.Condition as my custom rule which lets me get the theme. I then use the rule.Actions to get the datasource which lets me get the fields to be indexed.

This works when my rule is use datasource A for theme A and datasource B for theme B but I'm afraid it wont be robust with all rule combinations "hide", "except where" etc

Is there a way to use the sitecore's existing logic which determines what datasource to use given a personalisation rule?

Thanks

1 Answer 1

4

Not sure if this would work in your case, but previously i was able to run sitecore personalization rule by using this code to get the datasource (Sitecore 8.1):

public string GetPersonlizedRenderingDatasource(Rendering rendering, Item PageItem)
{
    var allReferences = GetRenderingsForControl(PageItem).ToList();
    var renderingUniqueId = ID.Parse(rendering.UniqueId);
    var renderingReferrence = allReferences.Where(i => ID.Parse(i.UniqueId).Equals(renderingUniqueId)).FirstOrDefault();

    if (renderingReferrence != null)
    {
       var ruleContext = new ConditionalRenderingsRuleContext(allReferences, renderingReferrence);

      renderingReferrence.Settings.Rules.RunFirstMatching(ruleContext);

      string personalizedDatasource = ruleContext.Reference.Settings.DataSource;
      return personalizedDatasource;
    }


 public RenderingReference[] GetRenderingsForControl(Item currentPageItem)
 {
     if (currentPageItem != null)
     {
         var device = Sitecore.Context.Device;
         DeviceItem deviceItem = Sitecore.Context.Database.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == "default").First();
         var renderings = currentPageItem.Visualization.GetRenderings(device, true);
         return renderings;
     }
     return new RenderingReference[0];
  }

I don't think this would work if used in your custom Search crawler, But will work if it's run within user page context.

2
  • Thanks I'll have a play with that. Might have to swap out the device stuff
    – JKerny
    Apr 8, 2018 at 23:18
  • 1
    Yes you can just grab the context device object as well, if this solution works for you, please mark it as a selected answer. Apr 8, 2018 at 23:25

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.