Monday, October 30, 2006

MOSS 2007 - Conditionally start a workflow with an eventhandler in SharePoint 2007

One of the major improvements in both WSS 3.0 and Microsoft Office SharePoint Server 2007 is the enhanced event handler framework. In SPS 2003 only document libraries supported event handlers, in WSS 3.0 and MOSS2007 there are also event handlers at site level and for all types of lists (both document libraries and lists). Another change is the support for both synchronous and assynchronous events - this means that an event also fires before an action occurs. There is for example an ItemAdded event as well as an ItemAdding event - the "Ed" event occurs after committing to the database and "Ing" event before committing changes to the database.

The base class for all eventhandling in SharePoint 2007 is the SPEventReceiverBase (Microsoft.SharePoint) class. The different classes you will need to inherit from to eventhandler are SPListEvenReceiver - for a list , SPWebEventReceiver - for site or site collection (deleted, deleting, moved, moving) and SPItemEventReceiver for individual items. In the next sample, I will create an event handler for a listitem which will start up a new workflow. To do this you will need to complete the next steps:

  • Create a new class library project in Visual Studio 2005
  • Add a reference to the Microsoft.SharePoint.dll to get access to the SPItemEventReceiver class
  • Add a custom class inheriting from the SPItemEventReceiver class
  • To get the event handler to work you will need to override some methods - in this case the ItemAdded and ItemUpdated methods
public override void ItemAdded(SPItemEventProperties properties)
{
ConditionalStartWorkflow(properties);
}

public override void ItemUpdated(SPItemEventProperties properties)
{
ConditionalStartWorkflow(properties);
}


private void ConditionalStartWorkflow(SPItemEventProperties properties)
{
try
{
SPListItem item = properties.ListItem;
string sDraft = item["Draft"].ToString();
bool bDraft = bool.Parse(item["Draft"].ToString());

//Start workflow only when it is not a draft version
if (!bDraft)
{
//Only start workflow - when there is no workflow active yet
if (item.Workflows.Count == 0)
{
SPWorkflowManager wfmgr = item.Web.Site.WorkflowManager;
SPWorkflowAssociationCollection wfassoccol = _
item.ParentList.WorkflowAssociations;

foreach (SPWorkflowAssociation wfassoc in wfassoccol)
{
//Find the GUID for the workflow association
if (String.Compare(wfassoc.BaseId.ToString("B"), _
"{c6964bff-bf8d-41ac-ad5e-b61ec111731c}", true) == 0)
{
wfmgr.StartWorkflow(item, wfassoc, "", true);
}
}
}

}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("StartHandler", ex.ToString());
}
}


  • Add a strong key to the library and build it. Then copy the dll you built to the Global Assembly Cache (GAC).

In SharePoint 2003 you could use the user interface to register an event handler, this is not possible anymore. There are however two other options:


  • Use the WSS features framework - this framework provides you with a way to package, deploy and activate your own customisations on top of SharePoint.
  • Do it through code (No UI as in WSS 2.0)

tags: , , , , , ,

5 comments:

Anonymous said...

Can you confirm that you have started a workflow using this?

Anonymous said...

I tried this piece of code. Earlier, I couldn't even start the workflow. Now, at least I can start it. But...it errors out right after starting...need to investigate why that is happening.

Anonymous said...

I have been succesfully buildin a feature and it runs good, but when i add a row from sql using proc_addlistitem the event does not fire, do you know what other stored procedure I must call in order to fire the event, If i add a row from sharepoint the feature works but from sql. thanks in advanced.

Anonymous said...

Is there a reason for not using Guid.Equals ?

if (Guid.Equals("{c6964bff-bf8d-41ac-ad5e-b61ec111731c}", wfassoc.BaseId))

instead of:

String.Compare(wfassoc.BaseId.ToString("B"), _ "{c6964bff-bf8d-41ac-ad5e-b61ec111731c}", true)

Anonymous said...

There is yet a third option to Add/Edit/Delete Event Handlers!

Check out Brian Wilson's Site

http://blogs.msdn.com/brianwilson/archive/2007/03/18/event-handlers-part-3-register-event-handlers-plus-free-site-settings-manage-event-handlers-add-on.aspx