Return to doc.sitecore.com

Valid for Sitecore 5.3.1
How to cause the workflow to invoke an action

A frequent question is whether it is possible to invoke an action when an item reaches some particular workflow state. Below we provide a step by step explanation and a code example concerning the subject in question.

Creating the class

  1. Create a class to deal with the workflow action.

    namespace OnAction
    {
       public class WorkflowAction
       {
          public void Process(WorkflowPipelineArgs args)
          {
             Item workFlowItem = args.DataItem;
             // Create a new version of the item and save it in a new item
             Item newVersion = workFlowItem.Versions.AddVersion();
             // getting the master database
             Database masterDB = Sitecore.Context.ContentDatabase;
             // getting desirable state that we want to assign in the new verison \
             // here in this example I worked with Edit Approve Workflow
             // but you can easily work with any exisiting workflow
             Item workflowState = masterDB.Items["/sitecore/system/workflows/simple/Done"];
             newVersion.Editing.BeginEdit();
             // setting workflow state to "Done" in our new version
             newVersion.Fields[FieldIDs.WorkflowState].Value = workflowState.ID.ToString();
             newVersion.Editing.EndEdit();
          }
       }

  2. Build the assembly and put it into the \sitecore\bin folder.

Setting up a new workflow command.

  1. First pick the workflow command that will be associated with your custom action:
    • Start the Content Editor and switch to System mode:

    • Unfold the Workflows folder and navigate the desired action:

  2. Create the child item e.g. for the Approve command, basing on System/Workflow/Action template.
    /upload/sdn5/faq/api/workflow_action_02.png
  3. Fill the Type field with the Type from your assembly.

    Type string field: class_namespace.class_name, assembly_name
    Parameters field: You can pass any parameters to your Process method. 

    /upload/sdn5/faq/api/workflow_action_03.png
    The specified method will be called each time the Approve command is executed.  

    NOTE: your class must have the Process method. This method will be called when the action is executed.