Fault Handling In SharePoint Workflows

Handling faults in WorkFlows is an important concept to consider because sometimes a routine that your workflow is inherently calling will just fart out. It just happens, sucks, but happens. It happens with generic .NET development, it happens when building custom workflows. An error will occur with your workflow, bubble up, and it must be handled appropriately. Typically, within a workflow, as an exception occurs, you want to do generally do one of two things, either exit the workflow and clean-up the persistence, or attempt to keep the persistence of the workflow.

Since a majority of the default activities that you are offered with the workflow foundation are pretty worthless (I am being negative since I had a bad day with them), you are going to end up writing a bunch of your own custom WorkFlow activities, in which case this becomes very important. The activity execution might fail, in which case an exception will bubble which has to be handled.

So how does one go about handling these dreaded faults as they occur within your WorkFlow code, and more importantly, when creating custom activities that are going to be tapped within a custom workflow?

I do this through the use of the ISharePointService interface and by overriding the HandleFault method. This is a good way to handle faults as they occur within your workflow activities The ISharePointService interface is one of the 4 local communication interfaces that you are afforded tapping into. There are also the three other communication interfaces that you can exploit, namely, IListService, ITaskService, and IWorkflowModificationService.

You will also see the use of ActivityExecutionStatus.Closed. This is because the clean-up work that is required before the activity within this code is assumed, and therefore I can close the execution status of the activity. It is possible to as well use ActivityExecutionStatus.Faulting is

When the HandleFault execution handler is dispatched by the runtime, it is expected that the activity will perform any cleanup work that is required prior to its transition to the Closed state. If your clean up work is short, you can do that and return ActivityExecutionStatus.Closed. If it is long, then return ActivityExecutionStatus.Faulting and wait for the required callbacks before ultimately creturning Closed.

  1. protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
  2. {
  3. ((ISharePointService) executionContext.GetService(typeof(ISharePointService))).LogToHistoryList(base.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.MinValue, string.Empty, string.Format("Your WorkFlow Farted!: {0}", exception.Message), string.Empty);
  4. return ActivityExecutionStatus.Closed;
  5. }

This is pretty simplistic, and not exceptionally impressive, however it is getting the job done for me and may save you some time while you are developing your custom WorkFlows :-).

share save 171 16 Fault Handling In SharePoint Workflows

9 Comments

  1. Vitaly Mogoreanu says:

    Thanks! That’s what I was looking for.

  2. PeterB says:

    Thanks!

  3. PeterB says:

    Thanks!

  4. Omarie Case says:

    This just save my hair. THANKS!!!!

  5. adam says:

    glad it helped!

    :)

  6. Prineeth says:

    Hi,

    Is there any way to handle error in WF only using SPD. Because we have some restrictions to use Visual Studio to do custom coding

  7. Sam says:

    Yes thank you so much ; i was trying to catch the error by fault handlers but it didnt work; your way work helped me to realize where is the problem exactly

  8. baskb says:

    Thanks mate. My custom reusable workflow farted too, but know I know why it stinks.

  9. Ghazanfar says:

    Really nice article, this 2 line funcation has save my life.

    Thanks Brother

Trackbacks/Pingbacks

  1. Sharepoint link love 06-22-2007 at Virtual Generations - [...] Fault Handling In SharePoint Workflows [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>