Blog

Workflow Extensions and Item Template for Debugging


by Tobias Lekman, 18 November, 2008

A huge issue around workflows in general is tracing and error handling. When a workflow encounters an error it will simply state "Error Occurred" within the workflow status column. The data is then written to the SharePoint log file.

Previously, I was using the Unified Log Viewer from CodePlex which I do recommend, but at times I found it hard to get on a live server's central administration site to do this.

To get around this, I am writing the error information straight to the workflow history list.

I have also added a method to ensure that the automatically inserted workflow column gets removed, as this can confuse end users and can be added within a separate, non-default view.

Method for logging errors to history list:

 1: /// <summary>

 2: /// Logs a message to the history list of the workflow instance.

 3: /// </summary>

 4: /// <param name="workflowProperties">The <see cref="SPWorkflowActivationProperties"/> to extend this functionality to.</param>

 5: /// <param name="message">The message to log to the history list.</param>

 6: public static void LogToHistoryList(this SPWorkflowActivationProperties workflowProperties,

 7: string message)

 8: {

 9: try

 10:     {

 11:         workflowProperties.Workflow.CreateHistoryEvent(

 12:             0,

 13: null,

 14: null,

 15: string.Empty,

 16:             message,

 17: string.Empty);

 18:     }

 19: catch

 20:     {

 21: return;

 22:     }

 23: }


Method for removing status column:



 1: /// <summary>

 2: /// Removes the workflow status column from the default view.

 3: /// </summary>

 4: /// <param name="workflow">The activity to extend this functionality to.</param>

 5: /// <param name="properties">The <see cref="SPWorkflowActivationProperties"/> from the workflow instance.</param>

 6: public static void RemoveStatusColumn(this Activity workflow,

 7:                                       SPWorkflowActivationProperties properties)

 8: {

 9: using (SPSite site = new SPSite(properties.SiteId))

 10:     {

 11: using (SPWeb web = site.OpenWeb(properties.WebId))

 12:         {

 13:             SPList list = web.Lists[properties.ListId];

 14:             SPView view = list.DefaultView;

 15:  

 16:             SPField field = list.Fields[properties.TemplateName];

 17: field.Hidden = true;

 18:             field.Update();

 19:  

 20: try

 21:             {

 22:                 view.ViewFields.Delete(field);

 23:                 view.Update();

 24:                 list.Update();

 25:                 properties.LogToHistoryList(

 26: "Workflow status column removed from default view on list.");

 27:             }

 28: catch (SPException)

 29:             {

 30: //Does not exist.

 31: return;

 32:             }

 33:         }

 34:     }

 35: }


If anyone knows of a better way of checking if the field exists already (the field name is completely random) then please let me know as this would be far better than letting the code throw an error on line 22. I have created an item template for Visual Studio 2008 that includes refactored, commented code and uses the two methods mentioned above.

Download the template and extension class.