Saturday 27 October 2012

Multisite workflow With Sitecore Rule Engine

Some company run multiple sites on single sitecore instance. sitecore run very smoothly. you can share templates, layouts etc......

All of sudden business requirement change and all content on each site must have to go through some kind of workflow.......... their own group of editors, so Sites cant share workflow they must have its own workflow.  developer : "PROBLEM.............PROBLEM......................." We cant share templates................etc etc

I have visited view blogs post many people saying that you can add one more steps in workflow and custom commnad will decide what work flow need to set on item. and I agree you can do.
But there is easy way you ca achieve this. "There is Sitecore there is way"...........Sitecore have one hiddle telent which is "Rule Engine", I really love it. Its amazing you can do lots of things with sitecore client...................Its really add to much power to Sitecore Product.


Let me show you how can you achieve this buy rule engine..

  1.  Go to /sitecore/system/Settings/Rules/Common/Actions                                                   Right click  Insert from template using /sitecore/template/system/rules/action Named:"Set workflow"  
           On right Text field Add

            Set WorkFlow [WorkflowId,Tree,root=/sitecore/system/Workflows,Item] State    [WorkFlowState,Tree,root=/sitecore/system/Workflows,Item]
    
            In type field(you can  change it later)
            Sominral.Rules.Action.SetWorkflow, Sominral.Workflow.Action
           

2. Create new folder under  /sitecore/system/Settings/Rules/

 Name: Item Created
Create another folder under Item Created  Folder

Then create new item using /sitecore/templates/system/rules/rule

On content area In Name field Set Workflow
 Now Click on Edit Rule



 
Is its easy you can set as many as rules you want.. there are lots of optin base on item layout base etc etc... all easy right ?
 
We have spedified code reference on Action item code will be  as below
 
namespace Sominral.Rules.Action
{
 using Sitecore.Rules;
 using Sitecore.Rules.Actions;
 using Sitecore.Diagnostics;
 using Sitecore.SecurityModel;
 using Sitecore.Data.Items;
 public class SetWorkflow : RuleAction where T : RuleContext
 {
  public string WorkflowId { get; set; }
  public string WorkFlowState { get; set; }
  public SetWorkflow()
  {}
  public override void Apply(T ruleContext)
  {
   Assert.IsNotNull(ruleContext, "rule context");
   if (!ruleContext.IsAborted)
   {
    using (SecurityDisabler securityDisabler = new SecurityDisabler())
    {
     var db = Sitecore.Data.Database.GetDatabase(ruleContext.Item.Database.ToString());
     var createdItem = db.GetItem(ruleContext.Item.ID);
     if (createdItem == null)
     {
      return;
     }
     createdItem.Editing.BeginEdit();
     using (new EditContext(createdItem))
     {
      createdItem.Fields["__Workflow"].Value = WorkflowId;
      createdItem.Fields["__Workflow state"].Value = WorkFlowState;
     }
     createdItem.Editing.EndEdit();
    }
   }
  }
 }
}
 
Yes we have added Action now how to trigged this action I know I know you are smart enough where to Trigger it dont you?......................
 
Answer is  Item Created event...........you need to add custom event on event pipeline. and trigger all rules under
/sitecore/system/Settings/Rules/Item Created/Rules item.

I know you can do it...........
hint: look how delete event fire.....


Thanks
Loving Rule engine..

Friday 12 October 2012

Sitecore Page Editor - An error ocurred when adding new control to placeholder.


Do you have problem to Add control to bottom of any placeholder in sitecore page editors  in sitecore Version 6.4.1 ?
Are you getting following error.


After spending an hour,  sitecore doesn't help much though.

Just did some debugging using dot peek find out that place holder names are case sensitive.
It will throw an error If place holder name is "Main" not "main" or "/contentArea/Right" not "/contentarea/right"

So make sure when you add control using content editor use all place holder name in lowercase and full path possible. else you could face above error.

It will throw above error.






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.