Showing posts with label Rule engine. Show all posts
Showing posts with label Rule engine. Show all posts

Thursday, 24 April 2014

Sitecore Rule : Item Saved Actions not showing.

I am really fan of sitecore rule engine and discover some hidden gems for rule engine. I am bit surprise that has sitecore intentionally not added separate templates for each rule type or left it for developer to add themselves. but good things is it works with new template type with same field names...

Created new rule using template: /sitecore/templates/System/Rules/Rule  under /sitecore/system/Settings/Rules/Item Saved/Rules but actions are not showing any action from /sitecore/system/Settings/Rules/Item Saved/Actions  and showing from common actions under /sitecore/system/Settings/Rules/Common/Actions






 
 

Why? its not showing Item Saved/Actions and Showing Common Actions.

Solutions: when we create rule new there is default insert option is "rule" using which use /sitecore/system/Settings/Rules/Item Saved/Rules template with no source specified as below:



By adding new source with item saved path not going to solve the issue and this will applied to all rules and showing item saved action in all rules like item deleted, item created if you customised  pipeline. so this solution not going to work. after digging more and other rules action come to know that you need to create new template same as item insert option or content editor warning etc.

Step 1. Duplicate Rule template in /sitecore/templates/System/Rules/Rule named "Item Saved Rule".
Step 2. Add /sitecore/system/Settings/Rules/Item Saved to source field to rule field.
Step 3: Add Insert option to Rule item  as Item Saved
Step 4 Create rule desire rule under item saved/Rules with new template created in step 1.
Step 5 Add desire action folder or action to item saved/actions.
After Applying this you will see as below.


Sitecore........is...........Amazing........................

 

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