سؤال

I was wondering whether it's possible to execute Sitecore workflow actions via the API. I can't use the approach from here: http://sdn.sitecore.net/Snippets/Workflow/Change%20the%20Workflow%20State%20on%20an%20Item%20via%20API.aspx (execute command that will trigger state change).

What I want is to programmatically create an item, and then set workflow state to "Awaiting for approval" for example, even if there is no command that has "Next Step" set to it.

Ideally, it would be something like

foreach (var action in awaitingForApprovalState.Actions)
{
  action.Execute();
}
هل كانت مفيدة؟

المحلول

Below is the code that set the item state and executes actions which are in this state:

public void MoveToStateAndExecuteActions(Item item, ID workflowStateId)
{
    Sitecore.Workflows.IWorkflowProvider workflowProvider = Item.Database.WorkflowProvider;
    Sitecore.Workflows.IWorkflow workflow = workflowProvider.GetWorkflow(item);

    // if item is in any workflow
    if (workflow != null)
    {
        using (new Sitecore.Data.Items.EditContext(item))
        {
            // update item's state to the new one
            item[Sitecore.FieldIDs.WorkflowState] = workflowStateId.ToString();
        }

        Item stateItem = ItemManager.GetItem(workflowStateId, 
            Language.Current, Sitecore.Data.Version.Latest, item.Database, SecurityCheck.Disable);

        // if there are any actions for the new state
        if (!stateItem.HasChildren)
            return;

        WorkflowPipelineArgs workflowPipelineArgs = new WorkflowPipelineArgs(item, null, null);

        // start executing the actions
        Pipeline pipeline = Pipeline.Start(stateItem, workflowPipelineArgs);
        if (pipeline == null)
            return;
        WorkflowCounters.ActionsExecuted.IncrementBy(pipeline.Processors.Count);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top