2

I need to display a field value only in a certain domain. Iam using rendering variant with below rule enabled but not able to see the value coming even in the domain 'sc9.local'.

enter image description here

2 Answers 2

2

You would have to create a custom condition for this. But its pretty simple. You can inherit from the StringOperatorCondition<T>:

public class RequestHostNameRule<T> : StringOperatorCondition<T> where T : RuleContext
{
    protected override bool Execute(T ruleContext)
    {
        Assert.ArgumentNotNull((object) ruleContext, nameof (ruleContext));
        if (string.IsNullOrEmpty(this.Value))
        {
            return false;
        }

        var service = ServiceLocator.ServiceProvider.GetService<IContext>();
        return this.Compare(service.Request.Host, this.Value);
    }

    public string Value{ get; set; }
}

That would do the check. Then you need to create a rule condition in /sitecore/system/Settings/Rules/Definitions/Elements/Request/Request Host Name

New Rule Condition

Once you have done that, you will be able to use that rule in your rendering variant:

enter image description here

Caveat

This will work fine for your local development machine, but in a production environment if the servers are behind a load balancer, the Request.Url.Host value may not be what you expect depending on how the load balancer has been configured.

1
  • Good call out on the CD scenario. Jan 9, 2019 at 16:52
1

The "domain" is your rule is not the domain name in the url - it is the Sitecore Security domain.. (try with "extranet" and you will see the field data). So that won't do what you want probably.

I'm not sure there actually is an ootb rule that matches the host name.. a list can be found here: https://doc.sitecore.com/developers/90/platform-administration-and-architecture/en/the-experience-management-personalization-conditions.html

You could match on site name (if that is sufficient). Or give your site a specific security domain (that way your current rule will work if you alter the value). Or write a custom condition..

Your Answer

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

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