简体   繁体   English

Sitecore:以编程方式将工作流分配给项目

[英]Sitecore: Assign workflow to an item programmatically

I have configured a workflow, starts with "Draft" state as usual. 我已经配置了一个工作流程,像往常一样以“草稿”状态开始。 And I have configured the workflow for standard values of the template. 我已经为模板的标准值配置了工作流程。 It works perfect in Content editor. 它在内容编辑器中工作得很好。 (When I create an item using the content editor, once I create the item, Workflow gets assigned to the item and it's state becomes "Draft".) (当我使用内容编辑器创建项目时,一旦我创建项目,工作流程就会被分配给项目,它的状态变为“草稿”。)

But when I create an item programmatically under the above template it does not assign the workflow. 但是当我在上述模板下以编程方式创建项目时,它不会分配工作流程。 What should I do to assign workflow? 如何分配工作流程? Please share any code samples if you have. 如果有,请分享任何代码示例。

Thanks. 谢谢。

Sitecore.Data.Items.TemplateItem template = this.MasterDatabase.GetItem("/sitecore/templates/user defined/sample types");
Sitecore.Data.Items.Item parent = this.MasterDatabase.GetItem(parentId); 

Sitecore.Data.Items.Item newItem;                
// Create new item and retrieve it
newItem = template.CreateItemFrom("sampleName", parent);

newItem.Editing.BeginEdit();

newItem.Name = StringFormatter.CreateItemNameFromID(this.newItem);
newItem.Fields["Title"].Value = "Sample Title"

newItem.Editing.EndEdit();

I would not use Dhanuka777's answer as it does not utilise Sitecore's Workflow State Commands and the useful functionality that comes with it eg email generation. 我不会使用Dhanuka777的答案,因为它不使用Sitecore的工作流状态命令及其附带的有用功能,例如电子邮件生成。 It also requires hardcoding Guids. 它还需要硬编码Guids。

Benefits of the below code: 以下代码的好处:

  • Utilise Sitecore's State Commands and the nice functionality they have eg generating emails to alert users etc 利用Sitecore的状态命令及其具有的良好功能,例如生成电子邮件以提醒用户等
  • Don't store the Guids of the States and Commands inside the solution 不要在解决方案中存储状态和命令的Guids
  • Independent of Sitecore's Context 独立于Sitecore的背景
  • Abstract so it can be used for all Workflows in Sitecore. 摘要,因此它可以用于Sitecore中的所有工作流程。
    public bool UpdateWorkflow(WorkflowState newWorkflowState, Item item)
    {
        Assert.ArgumentNotNull(newWorkflowState, "The new WorkflowState can not be null");
        Assert.ArgumentNotNull(item, "Item can not be null");

        bool successful = false;

        WorkflowState currentWorkflowState = GetWorkflowStateForItem(item);

        if (currentWorkflowState != newWorkflowState)
        {
            IWorkflow workflow = GetWorkflowOfItem(item);

            if (workflow != null)
            {
                List<WorkflowCommand> applicableWorkflowCommands = workflow.GetCommands(currentWorkflowState.StateID).ToList();

                foreach (var applicableWorkflowCommand in applicableWorkflowCommands)
                {
                    Item commandItem = _database.GetItem(applicableWorkflowCommand.CommandID);

                    string nextStateId = commandItem["Next state"];

                    if (nextStateId == newWorkflowState.StateID)
                    {
                        WorkflowResult workflowResult = workflow.Execute(applicableWorkflowCommand.CommandID, item, "", false);
                        successful = workflowResult.Succeeded;
                        break;
                    }
                }
            }
        }
        else
        {
            successful = true;
        }

        return successful;
    }

    public WorkflowState GetWorkflowStateForItem(Item item)
    {
        var workflow = GetWorkflowOfItem(item);
        return workflow != null ? workflow.GetState(item) : null;
    }

    public IWorkflow GetWorkflowOfItem(Item item)
    {
        return _database.WorkflowProvider.GetWorkflow(item);
    }

    private Database _database
    {
        get
        {
            return Sitecore.Data.Database.GetDatabase("master");
        }
    }

Solved the issue with Standard Fields, 解决了标准字段的问题,

newItem.Editing.BeginEdit();                    
newItem.Fields["__Workflow"].Value = "{4D1F00EF-CA5D-4F36-A51E-E77E2BAE4A24}"; //Set workflow
newItem.Fields["__Workflow state"].Value = "{7F39DF46-B4B9-4D08-A0D4-32DE6FD643D1}"; //Set   workflow state to Unposted.
newClassified.Editing.EndEdit();  

I looked all over for a good answer to this. 我到处寻找一个很好的答案。 I was creating a new item and wanted to start its workflow. 我正在创建一个新项目,并希望开始其工作流程。 jRobbins's answer didn't work for me because it throws an exception if the current workflow state of the item is null and it didn't offer a good way of setting the initial workflow state. jRobbins的答案对我不起作用,因为如果项目的当前工作流状态为null并且它没有提供设置初始工作流状态的好方法,则会抛出异常。

The following worked for me: 以下对我有用:

var workflow = Factory.GetDatabase("master").WorkflowProvider.GetWorkflow(workflowId);
workflow.Start(item);

This sets the workflow of my new item plus sets its workflow state to the initial state. 这将设置我的新项目的工作流程,并将其工作流程状态设置为初始状态。

After making sure I'd set a default workflow on the standard values, the following combination, with thanks to posters above, worked perfectly: 在确定我在标准值上设置默认工作流程后,以下组合,感谢上面的海报,完美地运作:

var workflowId = item.Fields["__Default workflow"].Value;
var workflow = Factory.GetDatabase("master").WorkflowProvider.GetWorkflow(workflowId);
workflow.Start(item);

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

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