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
- 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();
}
}
} - Build the assembly and put it into the \sitecore\bin folder.
Setting up a new workflow command.
- 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:
- Start the Content Editor and switch to System mode:
- Create the child item e.g. for the Approve command, basing on System/Workflow/Action template.
- 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.
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.