Saturday, June 19, 2010

Starting a Workflow from an Event Receiver - SharePoint 2007

Out of the Box there are only a few options to start a (custom) workflow. E.g. when an item is created or changed. This means that there are a lot of situations that a workflow is fired where it is not needed (often a workflow only needs to be started if a field has a specific value). Therefore we can write an event receiver to time better when a workflow is started. Below you find the code to start a workflow from your event receiver.

[code:c#]

public override void ItemUpdated(SPItemEventProperties properties)

{

if(YourCondition)

{

SPList parentList = properties.ListItem.ParentList;

SPWorkflowAssociation associationTemplate =

parentList.WorkflowAssociations.GetAssociationByName("Workflow Name",

new CultureInfo

(Convert.ToInt32(parentList.ParentWeb.RegionalSettings.LocaleId)));

SPSite siteCollection = properties.ListItem.ParentList.ParentWeb.Site;

siteCollection.WorkflowManager.StartWorkflow(properties.ListItem,

associationTemplate, String.Empty);

}

}

[/code]

No comments:

Post a Comment