SharePoint with Attitude…

    Karla Ponce's Blog

    Browsing Posts in Workflow

    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.

    I have a customized Approval workflow in Visual Studio, it creates a task per approver, approvers can complete their tasks through the InfoPath form in the browser and the workflow behaves as expected. No problems there.

    Now, from outside the workflow, I needed to complete some tasks programmatically if the task was not already completed by the approver.

    To do this, I thought of changing the status field of the Task item to “Completed”, as I would update a value for any other list item:

    It, in fact, modifies the task to Completed, nonetheless, it doesn’t seem to fire the OnTaskChanged() in the Workflow. By doing this, I could have all the tasks marked as Completed, and the workflow would still says “In progress”.

    I spent some time testing and realized that somehow that was not the correct way to update the Task, I had to find some other way.

    What I found, is that the correct way to update workflow tasks is by using the AlterTask method of the SPWorkflowTask class. This is the code:

      And done! Workflow tasks were completed and the Workflow itself was in status “Completed” after all the tasks were completed!