Sitecore Workflow命令抛出“找不到命令定义”错误

时间:2013-08-08 20:43:06

标签: c# workflow sitecore

在下面的代码中,以下行

WorkflowResult result = wf.Execute(SitecoreItems.MediaWorkflowApproveCommand, item, "", false); 

引发Could not find command definition错误。 ID和所有其他属性都有效,但命令定义无效。

关于可能导致它的原因的任何想法?

using (new SecurityDisabler())
            {
                // Find all related items 
                ItemLink[] itemLinks = dataItem.Links.GetValidLinks();
                foreach (ItemLink link in itemLinks)
                {
                    Item item = link.GetTargetItem();
                    // publishing related media items - the ones that were referenced by the workflow item 
                    // this can be extended - you can publish related aliases also 
                    if (item != null && item.Paths.IsMediaItem)
                    {
                        //push field to the next state
                        IWorkflow wf = item.Database.WorkflowProvider.GetWorkflow(item);
                        WorkflowResult result = wf.Execute(SitecoreItems.MediaWorkflowApproveCommand, item, "", false);
                    }
                }
            }

1 个答案:

答案 0 :(得分:5)

如果项目不处于任何工作流状态或此项目所在的工作流状态没有ID等于作为参数传递的命令ID的子项,则抛出此异常。

尝试执行以下代码:

        if (item.Database.Name == "web")
        {
            throw new Exception("Can not execute workflow command in web database");
        }
        if (String.IsNullOrEmpty(item[FieldIDs.WorkflowState]))
        {
            throw new Exception("Workflow state is not set for the item");
        }
        Item stateItem = ItemManager.GetItem(wf.GetState(item), Language.Current, Version.Latest, item.Database, SecurityCheck.Disable);
        if (stateItem == null)
        {
            throw new Exception("Workflow state " + item[FieldIDs.WorkflowState] + " is not a part of " + wf.WorkflowID + " workflow");
        }
        if (stateItem.Axes.GetChild(ID.Parse(SitecoreItems.MediaWorkflowApproveCommand)) == null)
        {
            throw new Exception("Workflow state " + stateItem.ID + " does not have a child command with id " + SitecoreItems.MediaWorkflowApproveCommand);
        }

执行行

之前
WorkflowResult result = wf.Execute(SitecoreItems.MediaWorkflowApproveCommand, item, "", false);