Gabbar
Gabbar

Reputation: 4066

Sitecore Workflow command throwing a "Could not find command definition" error

In the code below, the following line

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

is throwing a Could not find command definition error. The IDs and all the other properties are valid but the command definition is coming up invalid.

Any ideas as to what might be causing it?

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

Upvotes: 3

Views: 967

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27142

This exception is thrown either if the item is not in any workflow state or the workflow state which this item is in does not have any child with ID equals to command id passed as parameter.

Try to execute the following code:

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

before executing line

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

Upvotes: 5

Related Questions