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..

No comments:

Post a Comment