Sunday, June 10, 2012

Simple SharePoint 2010 Workflow Error Notifications

I've been working with SharePoint 2010 workflow for a while now, and I have to say I really enjoy the power it provides to automate a lot of otherwise tedious or error prone manual processes. And it's great to have the flexibility of authoring workflows in either SharePoint Designer or breaking out Visual Studio, depending on requirements and client preferences.

One of the things that's sadly lacking from SharePoint Designer workflows is the ability to satisfactorily handle errors when they occur during a running workflow. Ideally we'd have another container like the "step" and "impersonation step" sequence containers that supported a try-catch style block, where we could catch an error in the catch block and attempt to do something useful - like retrying an action, taking an alternative path in the workflow, or at the very least notifying someone that an error occurred. But we don't. Maybe in the next version.

What you do generally get when an error occurs though is an error log entry written to the nominated Workflow History list. In this case, the Event Type column indicates that the entry represents an error, and the Description column provides the details of the error. Given this, I wanted to see if it was possible to "listen" to the Workflow History list and send a notification email to a set of nominated recipients when an error occurred.

My first thought was to place an error-handling workflow on the Workflow History list itself, which would start whenever a new item was created, detect if the Event Type represented an error, and send an email... But hang on, don't we have Alerts built right into SharePoint?!

So why not create an Alert that's based on the Workflow History list, which sends an email whenever an error item is created? So that's just what I did, and here's how.

First step is to navigate to the Workflow History list in the browser. The easiest way I've found to do this is via the "Lists and Libraries" page in SharePoint Designer - just make sure you have the "Show catalog lists and system objects" option enabled in SPD Options.

Once you've browsed to the Workflow History list, open the "List Settings" page from the "List" tab and create a new calculated Yes/No column named something like Is Error, with a formula of "=OR([Event Type]=0,[Event Type]=10)". This column gives use a simple way to determine if the item represents an error or a normal log entry. Event Types 0 and 10 are the only 2 values I've encountered that seem to represent errors, although there could of course be more.

Save the new column, which returns you to the "List Settings" page.

Next step is to create a new view that provides us with a filtered list of only error items from the "Workflow History" list. Base the new view on the default "All Items" view, with a name like "Workflow Errors". You can add or remove whichever columns you like from the view, although the standard set is a pretty good starting point. The key though is to add a filter, where the Is Error column is equal to "Yes".

You might be wondering why we bothered to create a new column and use it in the view, rather than filtering directly by the Event Type column. Well, that's what I started with, but I found that I wasn't able to create an Alert based on a view that included a filter that used the Event Type column. Don't know why, and eventually I gave up worrying about it.

Save the new view, which again returns you to the "List Settings" page.

We're almost there. The final step is to create the Alert. Browse back to the "Workflow History" list itself, then select the "Alert Me - Set alert on this list" button from the "List" tab. Give the new Alert a meaningful name like "Workflow Errors" and select who will be notified and the delivery method (email or SMS). The most important parts though are to select the "Noew items are added" option for the "Change Type" setting, "Someone changes an item that appears in the following view" option and select your "Workflow Errors" view for the "Send Alerts for These Changes" setting, and the "Send notification immediately" for the "When to Send Alerts" setting.

Save the alert, and you should now be notified whenever an error occurs in your workflow! Of course there's more we could have done, like filtering by which workflow, or for specific errors, but it really depends on how you configure your Workflow History lists, but at least this is a start.