简体   繁体   English

以编程方式处理所有Sitecore项目与工作流程

[英]Programmatically Process all Sitecore items with Workflow

I have situation where I have a lot items in the Workbox that I need to clear out. 我有情况,我需要清理工作Workbox中的很多项目。

I need to process these items so that they can be assigned a workflow state (without triggering workflow actions) according to this rule: 我需要处理这些项目,以便根据此规则为它们分配工作流状态(不触发工作流操作):

If the item has a state of draft or waiting for approval and there is a published version higher than the current version then set the workflow state to be x worflowstate. 如果项目具有草稿状态或等待批准,并且发布的版本高于当前版本,则将工作流程状态设置为x worflowstate。

I haven't done much experimenting with workflow, so does anybody have any code samples or thoughts of how I could achieve this? 我没有做过很多实验工作流程,所以有没有人有任何代码示例或我如何实现这一点的想法?

Here is a blog post about Changing workflow state of Sitecore items programmatically . 这是一篇关于以编程方式更改Sitecore项目的工作流程状态的博客文章。

First you need to find all items in chosen workflow states: 首先,您需要找到所选工作流状态中的所有项目:

IWorkflow[] workflows = Sitecore.Context.Database.WorkflowProvider.GetWorkflows();

IWorkflow chosenWorkflow = workflows[..]; // choose your worfklow

WorkflowState[] workflowStates = chosenWorkflow.GetStates();

foreach (WorkflowState state in workflowStates)
{
    if (!state.FinalState)
    {
        DataUri[] itemDataUris = chosenWorkflow.GetItems(state.StateID);
        foreach (DataUri uri in itemDataUris)
        {
            Item item = Sitecore.Context.Database.GetItem(uri);
            /* check other conditions - newer version exists etc */
            ChangeWorkflowState(item, newWorkflowStateId);
        }
    }
}

The simplest code for changing workflow state of Sitecore item without executing any actions related to the new workflow state is: 用于更改Sitecore项的工作流状态而不执行与新工作流状态相关的任何操作的最简单代码是:

public static WorkflowResult ChangeWorkflowState(Item item, ID workflowStateId)
{
    using (new EditContext(item))
    {
        item[FieldIDs.WorkflowState] = workflowStateId.ToString();
    }

    return new WorkflowResult(true, "OK", workflowStateId);
}

public static WorkflowResult ChangeWorkflowState(Item item, string workflowStateName)
{
    IWorkflow workflow = item.Database.WorkflowProvider.GetWorkflow(item);

    if (workflow == null)
    {
        return new WorkflowResult(false, "No workflow assigned to item");
    }

    WorkflowState newState = workflow.GetStates()
        .FirstOrDefault(state => state.DisplayName == workflowStateName);

    if (newState == null)
    {
        return new WorkflowResult(false, "Cannot find workflow state " + workflowStateName);
    }

    return ChangeWorkflowState(item, ID.Parse(newState.StateID));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM