使用API​​执行Sitecore工作流状态操作

时间:2013-09-19 19:15:33

标签: api workflow sitecore sitecore6 sitecore-workflow

我想知道是否可以通过API执行Sitecore工作流操作。我无法使用此处的方法:http://sdn.sitecore.net/Snippets/Workflow/Change%20the%20Workflow%20State%20on%20an%20Item%20via%20API.aspx(执行将触发状态更改的命令)。

我想要的是以编程方式创建项目,然后将工作流程状态设置为“等待批准”,例如,即使没有设置“下一步”的命令。

理想情况下,它会像

foreach (var action in awaitingForApprovalState.Actions)
{
  action.Execute();
}

1 个答案:

答案 0 :(得分:6)

下面是设置项状态并执行处于此状态的操作的代码:

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);
    }
}