Skip to content

Instantly share code, notes, and snippets.

@karbyninc
Last active February 11, 2016 18:10
Show Gist options
  • Save karbyninc/95a6b55d9abfcb04982f to your computer and use it in GitHub Desktop.
Save karbyninc/95a6b55d9abfcb04982f to your computer and use it in GitHub Desktop.
Sitecore Development Insight - Custom Conditions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using Sitecore.Data.Items;
using Sitecore.Analytics;
using Karbyn.SC.Core.Extensions;
namespace Karbyn.SC.SmartStart.Website.Rules
{
    public class USRegionCondition<T> : WhenCondition<T> where T:RuleContext
    {
        public string RegionItemId { get; set; }
        protected override bool Execute(T ruleContext)
        {
            string region = Tracker.CurrentVisit.Region;
            if (string.IsNullOrWhiteSpace(region))
            {
                return false;
            }
            else
            {
                // Get the Item that has the places in the region
                Item regionItem = Sitecore.Context.Database.Items.GetItem(RegionItemId);
                if (null != regionItem)
                {
                    string regionValues = regionItem.GenerateTextFieldValue("Region");
                    if (!string.IsNullOrWhiteSpace(regionValues))
                    {
                        if (regionValues.ToLowerInvariant().Contains(region.ToLowerInvariant()))
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
}
// Get the Item that has the places in the region
Item regionItem = Sitecore.Context.Database.Items.GetItem(RegionItemId);
string regionValues = regionItem.GenerateTextFieldValue("Region");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment