c# - Sitecore Workflow 命令抛出 "Could not find command definition"错误

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

最佳答案

如果该项目不处于任何工作流状态或该项目所在的工作流状态没有任何 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);

关于c# - Sitecore Workflow 命令抛出 "Could not find command definition"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18135757/

相关文章:

c# - 如何为DataGridCheckBoxColumn创建通用编辑样式?

c# - 为什么延迟任务无法完全执行或阻塞 UI?

c# - 在现有接口(interface)上应用 ServiceContract 和 OperationContract

c# - 如何在 Sharepoint 站点工作流程 2010 中获取当前用户?

powershell - 使用 Sitecore Powershell 扩展更新媒体项目

javascript - $scwhead 未在面向营销人员的 Sitecore Web 表单中定义

c# - 带有数字键的动态 json 对象

workflow - AppFabric 工作流服务和工作流管理器的区别

visual-studio - 无法从Visual Studio 2017连接到Docker上的远程调试器

git - 使用 git 和 github 的正确工作流程