Friday, November 15, 2013

SharePoint 2010 State Machine Workflow error: "Failed to Start Workflow"

I've been working on a custom state machine workflow for a specific content type. I've associated the workflow to the content type like this:


This workflow has a custom initiation form, in which I'm passing some options to the workflow.

After doing the association to the content type I started getting the following error: "Failed to Start Workflow"

After doing a bit of debugging and head scratching I found that in the StartListWorkflow() method inside the initiation page in which the workflow association is retrieved from the workflowList.WorkflowAssociations list and it does not take into consideration the ContentType workflow associations.

private void StartListWorkflow()
 SPWorkflowAssociation association = this.workflowList.WorkflowAssociations[new Guid(this.associationGuid)];
        this.Web.Site.WorkflowManager.StartWorkflow(workflowListItem, association, GetInitiationData());
 SPUtility.Redirect(this.workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current);

So I replaced this line:

SPWorkflowAssociation association = this.workflowList.WorkflowAssociations[new Guid(this.associationGuid)];

with this line:

SPWorkflowAssociation association = this.workflowList.ContentTypes[workflowListItem.ContentTypeId].WorkflowAssociations[new Guid(this.associationGuid)];

in which I'm getting looking for the association in the WorkflowAssociations of the list content type of the workflowListItem.

No comments: