3

How can I change the "where the site name compares to value" personalization rule so that "value" is a list of Sites driven by my configuration?

I have a website in multiple languages with multiple hostnames. Each hostname corresponds to a different country and different site definition within my configuration (but each site points to the same place in the content tree). I have named my sites after countries, so:

  • mysite.com resolves to the "United States" site
  • mysite.co.uk resolves to the "Great Britain" site
  • mysite.ca resolves to the "Canada" site

What I am looking for is a way to change the "where the current web site name compares to value" to allow for a Dropdown List of "United States", "Great Britain", and "Canada" rather than a raw text field.

2 Answers 2

11
  1. Copy the /sitecore/system/Settings/Rules/Definitions/Elements/System/Web Site Name
  2. Put where the current web site name [operatorid,StringOperator,,compares to] [Value,Tree,root=/sitecore/system/Settings/Rules/Definitions/Elements/System/Copy of Web Site Name/Sites, value] into the Text field of /sitecore/system/Settings/Rules/Definitions/Elements/System/Web Site Name
  3. Implement your custom condition which can resolve the item by ID because by default WebSiteNameCondition expects your site name as a string

    using Sitecore;
    using Sitecore.Diagnostics;
    using Sitecore.Rules;
    using Sitecore.Sites;
    
    namespace MyProject.SiteConditions
    {
        public class WebSiteNameCondition<T> : Sitecore.Rules.Conditions.SiteConditions.WebSiteNameCondition<T> where T : RuleContext
        {
            protected override bool Execute(T ruleContext)
            {
                Assert.ArgumentNotNull(ruleContext, "ruleContext");
                if (string.IsNullOrEmpty(base.Value))
                {
                    return false;
                }
    
                var siteItem = Context.Database.GetItem(base.Value);
                if (siteItem == null)
                {
                    return false;
                }
    
                var siteName = siteItem["YourSiteField"];
    
                SiteContext site = Context.Site;
                if (site == null)
                {
                    return false;
                }
                else
                {
                    return this.Compare(site.Name, siteName);
                }
            }
        }
    }
    
  4. Put your implementation path to the Type field, in this case it is MyProject.SiteConditions.WebSiteNameCondition,MyProject

Result

You can select your website as an item in Rule Set Editor when you are using this newly created rule:

enter image description here

I suggest you to also read the following article to understand what are these 4 parameters for - https://community.sitecore.net/technical_blogs/b/sitecore_what39s_new/posts/creating-custom-conditions-part-1

6
  • 2
    1) Is it the name of the selected item that is taken as the value? 2) Is it a good practice to define the options under the condition item? Commented Dec 1, 2016 at 8:13
  • @DmytroShevchenko, that's true, I would like to check in the evening. Now it gets the ID of the selected page item. I will provide the corrected answer then after investigation. Commented Dec 1, 2016 at 13:53
  • Can I avoid using the standard template? I already have my countries defined in the tree somewhere else, and want to use a field off of each one.
    – jdylanmc
    Commented Dec 1, 2016 at 20:18
  • 1
    @DylanMcCurry Yes, you can use your own path. At the moment this answer is not a solution because when you select the site in Rule Set Editor then it writes the ID of the item as a value and this rule expect the site name as a string. I would like to provide you a full answer just I am still working today. Commented Dec 1, 2016 at 20:20
  • 1
    @DylanMcCurry, updated my answer. Commented Dec 1, 2016 at 22:04
3

I would not change the existing condition. Instead I would implement a custom condition that functions as you'd like. The following blog post explains in detail how to create a custom condition.

https://www.sundoginteractive.com/blog/how-to-customize-personalization-rules-in-sitecore-8

The condition you are going to copy is located here:

/sitecore/system/Settings/Rules/Definitions/Elements/Visit/Site Name

You'll need to accomplish every step described in the blog post but the main thing you need to do to display a drop-down to the Marketer instead of a text box is to configure your custom condition with the following syntax:

where the country name [operatorid,StringOperator,,compares to] [value,Tree,root=[[full Sitecore path to an item in your Sitecore tree that contains country items]],specific country]

What exposes the drop-down GUI instead of the textbox GUI is the Tree and root elements in the second half of the statement above.

2
  • This is great! How do I control which field I pull from my selected value?
    – jdylanmc
    Commented Dec 1, 2016 at 20:18
  • @DylanMcCurry, the answer provided by TRNKTMS seems complete to me. Should be everything you need.
    – DougCouto
    Commented Dec 1, 2016 at 22:53

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.