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 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 programmatically
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)

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 code block 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

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 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!
Comments are closed
Archived comments
Can a workflow created in SharePoint Designer 2007 and asssociated with a form library be started using this code?Thanks, I appreciate the article.
This is brilliant!
Hi An,
Thank you for your comment.
Tob.
Helped me .... thanks!
I'm glad you could use the info in the article Sue :-)
ghghh
I've used your event receiver code with success, thank you! However, one delima I have is that the document doesn't actually get deleted. I don't know whether that should be done in the workflow or the event receiver. If in the event receiver, I should think it would need to know the result of the approval workflow. If in the workflow, it would have to know that the item is being deleted. Is there a best way to do this?
Hi jnboud,
I guess that all depends on a variety of variables.
But if you were to do it from the receiver, you can always set a property on the item when the workflow finishes (which will trigger the ItemUpdated event) and if that property or field is set to whatever value you specify, delete the item. Just be aware that if you do this, you need secure logic in order to not end up on a catch-22 loop where the ItemUpdated executes a workflow, which executes ItemUpdates, which executes the workflow, which exe... You get it :-)
The other way, if you were to do it from the workflow, is perhaps that you specify a variable or property like above for your item/document and once the workflow picks it up, if the flag is set to true, it'll delete it in the final step?
All business logic is possible, but I guess it's a matter of getting it to match your requirements :-)
I hope this helps.
If you still need directions, please drop by www.mssharepointforums.com and create a new topic, I'm hanging out there as well.
Cheers,
Tobias.
Hi,
I have multiple workflow attached in a document library, all are sequential workflows. Depending on the column values for that particular item, one of the workflow will kick start. Can you please suggest where should I write the code. How can I achieve this.
Thanks
Hi MB,
Wouldn't you be able to design your workflows in such a way that each workflow checks the changed fields value, and if it is the one you're looking for in that perticular Workflow, you continue execution?
This way you don't have to involve external dependencies (no need for an event receiver or code to jumpstart the workflow if the logic is already in place).
So, having for example 5 workflows, they all kick off when an item is added/changed and only continue execution in the ones that match the field criteria.
If that's not possible, for some reason, then creating an event receiver for the list would be possible and then start the workflow from there.
Creating an event receiver: http://zimmergren.net/techn...
Jumpstart a workflow from a receiver: This article..
Regards,
Tobias.
Thanks a lot, it worked for me.
Monali
Hi Monali,
Thank you for getting back with the update, I'm glad it works :-)
Cheers,
Tobias.
Hello Tobias,
I am currently trying to use this code snippet for my client and the event receiver doesn't start the workflow. Please kindly look at my code and advice accordingly.
Hi Bamidele,
I suggest you debug the event receiver to see what happens, and also check the ULS logs.
This post is from 2009, and if you're working with a more modern version of SharePoint I'd probably recommending another approach.
Regards,
Tobias.
Thanks for your reply. I'm actually working on a SharePoint 2010 environment and the visual studio debugging helped me resolve the issues. I was somewhat confused with the GUID values and where they should be inserted in the code block.
Hi Bamidele,
I'm glad you got it working :-)
Cheers,
Tobias.
I want to achieve the similar kind of functionality to start 2013 workflow but getting null error in workflowassociations object i am using above code in event receiver on ItemAttach event:
public override void ItemAttachmentAdded(SPItemEventProperties properties)
{
// base.ItemAttachmentAdded(properties);
SPList parentlist = properties.ListItem.ParentList;
SPWorkflowAssociation associationtemplate = parentlist.WorkflowAssociations.GetAssociationByName("My 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);
}