Looking for something?

Author:

\Tobias Zimmergren\ (tobias at zimmergren dot net)

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!

0 Comments 25 January 2009
Tobias Zimmergren

Tobias Zimmergren

Hi, I'm Tobias. I am a Microsoft MVP for SharePoint and I use this site to share my thoughts on tech with you on topics like SharePoint, Office 365, Azure and general web development.

  Malmö, Sweden

Related Post

Comments powered by Disqus
res.