Mike Hodnick's Blog

COM Exception on Programmatic WSS Workflow Execution

Late last week I was running in to problems while attempting to programmatically execute a Windows SharePoint Workflow using the SPWorkflowManager StartWorkflow method:

//this is a very, very simplified example
SPSite site = 
	new SPSite("http://server/sites/site");
SPWeb web = site.RootWeb;
SPList list = web.Lists[0];
SPWorkflow workflow = list.WorkflowAssociations[0];
SPListItem item = list.Items[0];
SPWorkflowManager workflowManager = site.WorkflowManager;

SPWorkflow startedWorkflow = workflowManager.StartWorkflow(
	item, workflow, workflow.AssociationData);

When execution reached the StartWorkflow method, I received the following exception:

Microsoft.SharePoint.SPException: 
Exception from HRESULT: 0x8102009B ---> 
System.Runtime.InteropServices.COMException (0x8102009B): 
Exception from HRESULT: 0x8102009B

Goog'ling didn't yield much help.

By accident, I discovered that the workflow was being started for a list item that already had the same workflow started - but had errored during its execution. SharePoint decides to keep the workflow in a "running" state until a user (or developer) explicitly chooses to terminate it.

For example, here is a screen shot of a list item that has an "errored" workflow in progress:

running

By clicking on the "errored" workflow, you can terminate it on the next screen:

terminate

There is also a CancelWorkflow method on the SPWorkflowManager class that you can use to terminate the workflow programmatically.

As for why the workflow stays in an "error" state and doesn't terminate, I'm not sure yet.