2

I need to create a personalised rule that hides the component if the url contains a certain pattern and I'm a bit stuck in an early phase. I have two questions:

  1. What condition should my personalization rule class implement (is StringOperatorCondition ok?)? I assume have to read data from HttpContext.Current.Request, check if it matches my desired pattern and then returns true or false in the Execute() override method? Is this the case?

  2. Do I still need to add Conditions/Action in the Element Folder (/sitecore/templates/System/Rules/Taxonomy/Element Folder) since I don't want to parametrize the code behind?

pls dont yell I'm learning ty

3

When creating the code for your custom personalization rule it should implement StringOperatorCondition class:

public class CustomQuerystringCondition<T> : StringOperatorCondition<T> where T : RuleContext
    {
        public string parametervalue { get; set; }
 
        protected override bool Execute(T ruleContext)
        {
            Assert.ArgumentNotNull(ruleContext, "ruleContext");
            var param = this.parametervalue;
            var paramFromUrl = HttpContext.Current.Request.QueryString["param"];
            if (param == paramFromUrl)
            {
                return true;
            }
            return false;
        }
    }

In order for your rule to appear in the components Personalize section you need to create custom Element and personalization condition under /sitecore/system/Settings/Rules/Definitions/Elements and make sure to select a tag in the Tags folder in order for your custom rule to appear in the appropriate group.

You can also create a custom tag if you want to have your rule appear in a different section in the Personalize rules list view by following the steps described https://ankitjoshi2409.wordpress.com/2018/09/03/sitecore-how-to-create-custom-personalization-rule/

1

Mersi Cristi, very helpful.

To complete the answer, this is the personalization Class body. No prop was needed since there are no parameters from the customer (Q1):

public class UrlContainsProductIdRule<T> : StringOperatorCondition<T>
where T : RuleContext
{
    protected override bool Execute(T ruleContext)
    {
        Assert.ArgumentNotNull(ruleContext, "ruleContext");

        return HasProductId(HttpContext.Current.Request.Url.AbsoluteUri);
    }

    private bool HasProductId(string url)
    {
        // process if URL contains the product id [000-999]
        string regexPattern = @"[0-9]{3}-[0-9]{3}";

        return Regex.IsMatch(url, regexPattern);
    }
}

Q2: Do I still need to add Conditions/Action in the Element Folder (/sitecore/templates/System/Rules/Taxonomy/Element Folder) since I don't want to parametrize the code behind?

The answer is "Yes" because Condition item I add under Element Folder, has a section "Script". In the Type field has to connect to the code behind, via NameSpace.ClassName,dllName.

I hope this answer is anwers my own question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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