search blog
most popular
MCP MCTS MCT MVP

Starting a SharePoint workflow from code (Event Receiver) 

Author: Tobias Zimmergren
URL: http://www.zimmergren.net

In one of my current projects I was facing the issue of triggering a workflow - but didn’t want the entire workflow to kick off unless absolutely necessary.

Instead of the traditional approach where you hook up your workflow to trigger on new items or items being changed, I want to make a check to see if anything really had changed and then check a few other variables before even starting the workflow at all.

This resulted in me creating an Event Receiver which then could take care of the logic to see if the workflow should start or not. (Note that you’ll have to assess if this approach is doable in your situation, as it might not be applicable in all scenarios)

With this post I presume that you’ve got basic knowledge in creating a workflow and event receivers and will simply jump to the place in the code where my receiver takes care of the validation stuff and then (if validation == okay) starts the workflow.

Start a SharePoint workflow programatically

In the Workflow project

First of all, in your workflow project (In my case it’s a SharePoint Sequential Workflow) – look for the following line of code as shown below (usually in the AssemblyInfo.cs file) and remember and/or note this GUID. This is the GUID we need to know in order to identify our workflow from the Event Receiver

Locate and note the GUID of your WF (AssemblyInfo.cs)
image

Note; In my Workflow project, I choose not to hook the workflow up with “Start at item change” since I don’t want the WF to start (at all) unless I have validated a few variables first (done in the Event Receiver)

In the Event Receiver project

Complete codeblock for the Event Receiver
My code looks like this but the custom logic for validating weather to start the WF or not, has been removed 
image

The important part we need to focus on is the wfManager.StartWorkflow() method, which is the code that actually starts the workflow if certain criteria's are met as per the checks above.

Summary

I wrote this post as a reminder to myself when I need to kick-start a workflow somehow, but I do hope it can help someone else out there as well.

Have a great weekend!

 
Posted on 25-Jan-09 by Tobias Zimmergren
7 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags:
 

Links to this post (Trackbacks/Pingbacks)

Comments

Monday, 26 Jan 2009 08:31 by Kanwal Khipple
Thank you for this code. It will go into code repository!

Monday, 26 Jan 2009 03:22 by Shereen
Hi Tobias, this is a great post, very handy. I agree, you may not always have a need for this, but I can think of a couple scenarios in the past where this would have been a good approach. thanks for sharing!

Monday, 26 Jan 2009 08:13 by Rinwal B
WorkflowManager of course! good code block to loop through the stuff and check for right workflow.. had problems with this but i think iut works now. manu thanks

Saturday, 31 Jan 2009 11:02 by Dave W
Absolutely brilliant. had problem with this for long time - working good even in evenreciver

Monday, 1 Jun 2009 01:05 by mark
This is exactly what I need with only one problem. As soon as my workflow has started I need to redirect the page to a custom dispForm that has a webpart that acts as a new form for a details list. As soon as I call SPUtility.Redirect it exits the thread, and the workflow fails. If I comment out the redirect, then the workflow starts fine. Is there anyway I can call the workflow without looking it on the redirect? I have tried putting the workflow in it's own thread, but I still get the same problem.

Wednesday, 5 Aug 2009 09:49 by John P
How to deploy that please? A 100 posts on how to code an Event Receiver, and not a single one on how to deploy!

Name:
URL:
Email:
Comments: