Looking at a document library with workflows, I noticed that one of the instances of a workflow was in status “Error Occurred”. All of the tasks were Completed, but for an error in the code, the workflow didn’t end as expected and was showing the error status.
The error itself was not a problem, it was fixed in the next version of the workflow, and for future instances, the workflow will end correctly. Nonetheless, the client didn’t want the error message to be displayed in the workflow.
So what I needed to do was to change the workflow status. Using the user interface, the only option we have is to cancel the workflow. Thing that we didn’t want to do because not only we didn’t want to cancel the workflow (we wanted to complete it) but because canceling the workflow deletes all the tasks associated with the workflow, and of course that information needed to remain there.
Immediately I looked at the object model, and to my surprise I didn’t find anything to be able to change the status of a workflow. The SPWorkflowManager let me Cancel the workflow, but that’s it, and again, we didn’t want to cancel the workflow, we wanted to finished correctly.
After some research, and spending way too much time on this (thanks Microsoft!), I found that the way to change the status of a workflow is by using the AlterTask method of the SPWorkflowTask class, as if updating a Workflow Task, the only difference is the inclusion of this line of code:
Done! Workflow Completed!
The complete code to update the task is:
Refer to my post Complete Workflow Task Programmatically to know more about updating workflow tasks.