Search code examples
episerver

Check if block uses personalization


Is it possible to programmatically determine if the content of a block uses personalization?

I need to know in order to handle caching correctly when the content of a block can be personalized for different visitor groups.


Solution

  • In order to make decision about personalization you will need to get access to ContentAreaItem instance itself.

    The most easiest way to get access to ContentAreaItem is to hook into rendering pipeline. You may need to swap out ContentAreaRenderer (see AlloyTech sample site for code snippet) with your own implementation.

    Then you can implement BeforeRenderContentAreaItemStartTag:

    public class MyCustomContentAreaRenderer : ContentAreaRenderer
    {
        protected override void BeforeRenderContentAreaItemStartTag(TagBuilder tagBuilder, ContentAreaItem contentAreaItem)
        {
            var isPersonalizationApplied = contentAreaItem.AllowedRoles.Any();
        }
    }
    

    Next question of course is how you will deliver this decision down to block type instance, as overridden ContentAreaRenderer method is far enough from the block instance inside content area.