Workflow Event Receiver in SharePoint 2010

In SharePoint 2010, we now have new receivers and one of them is workflow event receivers. We can handle different events with respect to the workflows that triggers in SharePoint.

You can handle multiple events with regards to the workflow. These are:

1)    When workflow is starting
2)    When workflow is started
3)    When workflow is completed
4)    When workflow is postponed

As name suggests you can trap different events of the workflow. 

These come handy if you have requirement to start series of workflow based on completion criteria from one workflow to another. If one workflow completes and then you want to start another workflow in chain, that can be done now by trapping these events.

This is only for list or library workflows and not for the site workflow.

Here is an example with code.

/// <summary>
    /// List Workflow Events
    /// </summary>
    public class EventReceiver1 : SPWorkflowEventReceiver
    {
       /// <summary>
       /// A workflow is starting.
       /// </summary>
       public override void WorkflowStarting(SPWorkflowEventProperties properties)
       {
           SPWeb Web = properties.ActivationProperties.List.ParentWeb;
           
           SPList lstTracking = Web.Lists["Track Different Events"];                 

           SPListItem item = lstTracking.Items.Add();

           item["Title"] = "Starting the workflow";

           item.Update();          

       }

       /// <summary>
       /// A workflow was started.
       /// </summary>
       public override void WorkflowStarted(SPWorkflowEventProperties properties)
       {
           using (SPSite site = new SPSite(properties.WebUrl))
           {
               using (SPWeb Web = site.OpenWeb())
               {

                   SPList lstTracking = Web.Lists["Track Different Events"];                

                   SPListItem item = lstTracking.Items.Add();

                   item["Title"] = "workflow has been started";

                   item.Update();
               }
           }
           
       }

       /// <summary>
       /// A workflow was completed.
       /// </summary>
       public override void WorkflowCompleted(SPWorkflowEventProperties properties)
       {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb Web = site.OpenWeb())
                {
                    SPList lstTracking = Web.Lists["Track Different Events"];                   

                    SPListItem item = lstTracking.Items.Add();

                    item["Title"] = "workflow has been completed";

                    item.Update();
                   
                }
            }              

       }
    }

You can also handle several completion type enumerations in WorkflowCompleted method that helps you to execute certain code based on the completion criteria.


You can also check completion type enumerations in WorkflowCompleted method.