It was a good idea from one of our client to show a highlight directly on the renderings which use any Personalization Rules in Experience Editor.
I found out to create a HTML extension method for it and then I can include it to renderings. Then I can call it like this:
<div> | |
@Html.RenderPersonalizationRuleHightlight() | |
</div> |
And here is the called HTML extension method:
public static HtmlString RenderPersonalizationRuleHightlight<T>(this HtmlHelper<T> htmlHelper) | |
{ | |
var result = new MvcHtmlString(string.Empty); | |
if (!Sitecore.Context.PageMode.IsExperienceEditor) | |
{ | |
return result; | |
} | |
var sitecore = htmlHelper.Sitecore(); | |
if (sitecore.CurrentRendering == null || sitecore.CurrentRendering.Properties == null) | |
{ | |
return result; | |
} | |
string rulesString = sitecore.CurrentRendering.Properties["PersonlizationRules"]; | |
if (string.IsNullOrEmpty(rulesString)) | |
{ | |
return result; | |
} | |
try | |
{ | |
var rules = XElement.Parse(rulesString); | |
var elements = rules.Descendants().Elements("rule").ToList(); | |
if (elements.Count() > 1 || elements.Any(e => e.Attribute("uid").Value != ID.Null.ToString())) | |
{ | |
result = new MvcHtmlString("<div class='page-editor-dach-highlight'>This rendering uses personalization rules.</div>"); | |
} | |
} | |
catch (Exception e) | |
{ | |
Sitecore.Diagnostics.Log.Error("An error occured: ", e, typeof(HtmlHelperExtension)); | |
} | |
return result; | |
} |