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.

HTH!

13 comments:

  1. Nice article Dave. I found it useful for something I am doing but rather than sending an email, my solution will be presented as a webpart on an admin page. The list will be filtered to items that occurred within the last 7 days.
    Thanks for the heads up.
    Patrick.

    ReplyDelete
  2. Very Clever! Thanks! Collin Burnell

    ReplyDelete
  3. OK, I am attempting to take this to the next level...
    On my DEV site, I have added your 'Is Error' Calculated Column as a Site Column and managed to add it to the (_hidden) 'Workflow History' Content Type. It is taking a while to push this column out to the Sub-Sites. If I am successful, I am going to create a reusable workflow for this content type with the goal of having the entire Site Collection notify me of any errors that occur. Stay Tuned. Collin Burnell.

    ReplyDelete
    Replies
    1. I have this setup on two of our main productions applications and very nice. I would like to use your idea Collin on existing and new sites so that we are always in the know. I was wondering if have you been able to implement your idea and how it is working

      Delete
  4. This is great. I have used it and is saved a bunch of time...Great Idea! Thanks

    ReplyDelete
  5. Excellent work. Exactly what I needed. Thank you SO much for taking the time to share.

    ReplyDelete
  6. Check this link for workflow event type...
    http://www.spjeff.com/2010/01/29/workflow-history-event-type-numbers-decoded-2/

    ReplyDelete
  7. Another method is to have a second workflow attached to the same list or library as the 1st workflow set to auto run at item creation. The 2nd workflow pauses at the start for a few minutes (long enough for the 1st wf to complete) and then checks the status of the main workflow by testing if = x where x will be 2 for still in progress, 3 for an error occurred, 5 for completed or 6 for failed on start. I haven't found any other values.
    Pretty primitive but it does allow you to trap expected error conditions.

    ReplyDelete
    Replies
    1. That is to test the (mainworkflowname) = x

      Delete
  8. I don't have the "Someone changes an item that appears in the following view" option in my list alert config... why?!?! :(

    ReplyDelete
  9. Thanks for that wonderful write-up...

    ReplyDelete
  10. Old post but still helpful - thanks!

    ReplyDelete