2

我想知道是否可以通过 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();
}
4

1 回答 1

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);
    }
}
于 2013-09-20T07:05:08.187 回答