Wednesday 16 May 2012

Custom Sitecore Rule engine conditions

I was working on sitecore project where I need to show Insert option depend on what value they selected in parent item.content editor so Auther cant add wrong item in tree. Sitecore give flexibility to extend that functionality with writing minimum code possible.

Create class name public class WhenParentField<T> inherited from  stringOperatorCondition<Twhere T : RuleContext

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
 
namespace Namespace
{
    public class WhenParentField<T> : StringOperatorCondition<Twhere T : RuleContext
    { 
        public string FieldName { getset; } 
 
        public string Value { getset; }            
 
        public WhenParentField()
        {
        }
 
        protected override bool Execute(T ruleContext)
        {
           Assert.ArgumentNotNull(ruleContext, "ruleContext");
           Item item = ruleContext.Item.Parent;
            if (item != null)
             {
               if (!string.IsNullOrEmpty(this.FieldName))
               {
                  string @value = this.Value;
                  string empty = @value;
                  if (@value == null)
                  {
                      empty = string.Empty;
                  }
                  string str = empty;
                  string item1 = item[this.FieldName];
                  return base.Compare(item1, str);
               }
              else
              {
                return false;
              }
          }
         else
         {
             return false;
         }
       }
    }
}

Now compile your project.

In  sitecore content Editor go to "Sitecore/system/settings/Rules/common/Conditions and create new rule using "/sitecore.templates/system/rules/conditon called "When Parent Field" and save it.

 In Text field copy "where Parent [fieldname,,,specific] field [operatorid,StringOperator,,compares to] [value,,,specific value]" and in Type field "Namespace.WhenParentField,assembly name"; and save it.

 condition is created now time to apply rule for insert option . To do that
Now  go to "/sitecore/system/Settings/Rules/Insert Options/Rules" and create new item using "/sitecore/templates/System/Rules/Insert Options Rule" template
 and name the rule according to your need. in Rule field click edit and it open Rule set Editor window
 

and select where item has specific template and new created common rule  where parent specific field compares to specific value.now select action what happen if both condition are true. so I have selected insert specific insert otpion.

This give rule based insert option depending on parent items field value. obiously this wont just work in real word  incase some one added item before selecting  parent item then what happen to resolve this issue i have wrote some item validtion rules so every time when item saved it checkd and notify  auther and wont published due to item validtion error.

I will show that in my next post. Thanks for viewing.

Refence sitecore Rule engine cookbook.