Starting a Sharepoint workflow programmatically turned out to be not as easy as it should have been (but that was due to an improper Sharepoint configuration).  It is actually pretty easy to do once you stumble through the Sharepoint API''s with a little trial and error.

To start a workflow programmatically you'll have to do the following:

  • Determine the GUID, or index value, of the workflow you want to start.
  • Determine the AssociationData that the workflow requires to start.

Easy enough!

Ok Mr. Smarty-Pants, how do I determine the GUID of the workflow I want to start?

There are a few ways you can do this.

You could iterate in code through all the associated workflows of a document library and use the debugger to find out the GUID of the workflow you want.

Or an easier way is to right click on an item in your doc library and select "Workflows" from the dropdown menu.

 image

You'll then see a list of all the workflows associated to your document library.  Go ahead and click on one of them.

image

In the address bar you'll see a query string variable called "TemplateID" and the data after that is your GUID.  In this case, its "a5d937a-4d61-4a2a-ab2e-5956d77f62d2".  Alright so now we have our Workflow GUID we have to create a SPWorkflowAssociation object with that GUID.

    1                 //get the workflow associated with this doc lib and kick if off manually

    2                 SPWorkflowAssociation wrkFl = docLib.WorkflowAssociations[new Guid("guid of workflow")];

Once we have an SPWorkflowAssociation object, we can then begin to work with a specific workflow and set it to start on specific list item.

    1 //get the workflow associated with this doc lib and kick if off manually

    2 SPWorkflowAssociation wrkFl = docLib.WorkflowAssociations[new Guid("guid of workflow")];

    3 site.WorkflowManager.StartWorkflow(fileToUpload.Item, wrkFl, wrkFl.AssociationData, true);

The way to tell a workflow to start is to go through the WorkflowManager of our SPSite object.  We call the StartWorkflow method of the WorkflowManager and pass in our list item to act on(SPListItem object), the workflow to use (SPWorkflowAssociation object), any workflow start data (string object), and whether or not the workflow is an autostart workflow (optional boolean).

The SPListItem object is pretty easy to understand.  In the code above I was using a file that I was uploading to document library.

The SPWorkflowAssociation is the object we created on the line above it and is the workflow that we want to act on our SPListItem object.

The string of start data is a little less easy to understand at first glance.  Every workflow expects a string of well formed XML data as its start or "Association" data.  This XML data will be different for each workflow.  The screenshot below is the AssociationData that an Approval workflow expects to be passed in.

image

In some cases, you may have to modify this XML and pass in certain values to the workflow that are required for it to start properly, but you'll need to determine that on your own for your specific case.  One thing to note is that I don't believe passing in Null or an Empty string will suffice for this variable.  If anything, just pass the AssociationData property of the workflow association and that should work most of the time.

The final variable is optional and it specifies whether or not the workflow is an AutoStart workflow or not.  I was able to get all my code working by specifying this variable...Some people have gotten it to work without this variable, so play around and find out what works for your specific case.

Here is a quick chunk of code to loop over all the items in a document library and kickoff a workflow for each one.

foreach (SPListItem itm in docLib.Items)
{
    //get the workflow associated with this doc lib and kick if off manually
    SPWorkflowAssociation wrkFl = docLib.WorkflowAssociations[0];
    site.WorkflowManager.StartWorkflow(itm, wrkFl, wrkFl.AssociationData, true);
}

 

I did manage to run into an error with the code above that was due to an improper Sharepoint Configuration.  Check out my post about Workflow stuck in "Starting" state to see how I solved that problem.

Also as specified above, the AssociationData string cannot have Null or an Empty string passed into it, it needs at least something to start.


Posted in:   Tags:

Comments


Germany Michael
January 23. 2008 23:22
Michael
Hello Tony,

is it possible to get the whole sourcecode of your application. I have some problems to understand your example.

no site


Australia YodaFromDownUnder
August 6. 2008 19:49
YodaFromDownUnder
when finding the workflow, if you dont want to refer to the GUId, you could also use -

SPWorkflowTemplate baseTemplate = web.WorkflowTemplates.GetTemplateByName("Approval",CultureInfo.InvariantCulture);

SPWorkflowAssociation assoc = SPWorkflowAssociation.CreateListAssociation(baseTemplate, "My Approval Workflow",web.Lists["Workflow Tasks"], web.Lists["Workflow History"]);    

I have one dilemna though. There's a workflow I have which was created by a third party company. It's deployed, i've got the dll and features.xml and all but when trying to add it automatically to a list I have no idea what to use as the AssociationData XML. It does have a custom association form with parameters but I don't really know if i'm forming my XML correctly and with the correct names. Do I have any options other than asking them for the parameter names/types?

no site


United States Austin
August 14. 2008 10:26
Austin
I've managed to start an Approval workflow using this informatino and everything is working fine, except that if someone rejects the approval request the workflow continues when I need it to stop.  I've done everything I can think to do with the StopOnAnyReject tag and it doesn't make a difference.  Any ideas?

Thanks!

no site


United States mkzp9
September 4. 2008 06:43
mkzp9
Hi,

Can you tell me how the approvers of the work flow can look at a document, verify the details on the document before they actually approve it. Also, once it is finally approved the details on the form should be entered into the active directory!!!

no site


United Kingdom Priyanka
September 4. 2008 20:56
Priyanka
Hi Tony,

My question is that I can start the workflow programmatically, but where actually place this code. Another point is, workflow is associated with some list, and creation or modification of any item in the list would initiate the workflow. But what if I have to initiate the workflow based on occurance of some event for example End of Quarter? Can I achieve it?

Also I want to initiate workflow not for all items in a list, but only a specific item, can I do that?

no site


United States s98ssr
October 29. 2008 10:29
s98ssr
I have an .aspx page and user chooses a date and clicks on Submit.  The click event now runs and calls a stored procedure with the given date and executes an ssis package programmatically and generates a file which is then added to the document library.  

I would like to first manually start a workflow (from Initiation form that would get the date from the user) which would then call the stored procedure. In other words, one of the tasks in the workflow is to execute the ssis package.  

Any ideas on how to go about doing this?  I guess I need to start the workflow programmatically or are there ways through the Share Point Designer to achieve this?  Any help is greatly appreciated.  Thanks!!

no site


January 18. 2009 11:54
pingback
Pingback from keyongtech.com

InvokeWorkflowActivity for MOSS Approval Workflow | keyongtech

http://www.keyongtech.com/1978213-invokeworkflowactivity-for-moss-approval-workflowhttp://www.keyongtech.com/1978213-invokeworkflowactivity-for-moss-approval-workflow


January 27. 2009 12:59
pingback
Pingback from blog.lulutech.com

kevin Mocha - Start a Sharepoint Workflow Programmatically

http://blog.lulutech.com/PermaLink,guid,d5e6c386-34c6-4605-aef4-8a1204add49a.aspxhttp://blog.lulutech.com/PermaLink,guid,d5e6c386-34c6-4605-aef4-8a1204add49a.aspx


June 19. 2009 13:19
Lån Penge
Thanks a lot for sharing this. Definately bookied Smile

http://www.laan-penge.net/http://www.laan-penge.net/


July 5. 2009 16:29
club penguin cheats
Can you tell me how the approvers of the work flow can look at a document, verify the details on the document before they actually approve it. Also, once it is finally approved the details on the form should be entered into the active directory!!!

http://www.club-penguin.org/http://www.club-penguin.org/


July 8. 2009 14:45
pingback
Pingback from blog.qumsieh.ca

How To Programmatically Trigger a Custom Workflow | SharePoint Fun

http://blog.qumsieh.ca/2009/07/08/how-to-programmatically-trigger-a-custom-workflow/http://blog.qumsieh.ca/2009/07/08/how-to-programmatically-trigger-a-custom-workflow/


January 16. 2010 17:46
small bathroom designs
Hi,
Its very greatful for choice BlogEngine.Net as platform your blog. I'm using blogengine too and for me thats easy to manage althought I'm newbie.
happy writing

Regards,
fat

http://www.bathroomvanityfurniture.net/small-bathroom-designs.htmhttp://www.bathroomvanityfurniture.net/small-bathroom-designs.htm


January 17. 2010 18:07
McFarlane
The way you have stated to start it programmatically is effective. Here are our McFarlane Toys company we have been working with it for some time but couldn't figure this part out until you came along.

http://www.toysportsfigures.com/http://www.toysportsfigures.com/


January 17. 2010 22:42
boat charters
Nice article.Thank you.

http://www.sydneysidercruises.com/http://www.sydneysidercruises.com/


January 18. 2010 16:06
business opportunities
Great post, I look forward to reading more.

http://www.mapofsuccess.com/http://www.mapofsuccess.com/


January 18. 2010 18:36
Web Templates
Thanks for sharing your thoughts. It was really an awesome post. I find it  to be an interesting and informative topic to be discussed.

http://www.free-dreamweaver-templates.org/Choose_web_templates.htmlhttp://www.free-dreamweaver-templates.org/Choose_web_templates.html


January 24. 2010 20:16
latest casinos online
i found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me.Really the blogging is spreading its wings rapidly

http://www.latest-casinos.com/http://www.latest-casinos.com/


January 25. 2010 07:20
cash loans
The quality of your work, in the long run, is the deciding factor on how much your services are valued by the world.

http://cashusloans.com/http://cashusloans.com/


January 25. 2010 20:27
cisco certification raleigh
I posted your blog to my facebook group


Regards

Joansina

http://www.nhrtp.com/http://www.nhrtp.com/


January 25. 2010 23:27
Conservatories Designs
Great nice post I like it so much.

http://www.evergladetrade.co.uk/conservatories.htmlhttp://www.evergladetrade.co.uk/conservatories.html


January 26. 2010 23:19
Winnipeg website development
I would love to read more about this topic.Don’t stop blogging! It’s nice to read a sane commentary for once.

http://alivewebdesign.com/winnipeg-website-design.htmhttp://alivewebdesign.com/winnipeg-website-design.htm


January 29. 2010 15:25
Life insurance
You really know your stuff... Keep up the good work!

http://www.ratedetective.com.au/insurance/life-insurancehttp://www.ratedetective.com.au/insurance/life-insurance


United States SEO
January 29. 2010 16:33
SEO
Hi,

Thanks heaps to the author!

http://www.webmarketingexperts.com.au/http://www.webmarketingexperts.com.au/


January 29. 2010 16:47
adult sex toys
I feel a lot more people need to read this, very good info!

http://www.diamonddooronline.com.au/http://www.diamonddooronline.com.au/


January 31. 2010 17:45
Apex Professionals LLC
This is a cool screen idea ! It is very interesting indeed.Thank you for your info.i love to read all info.This article gives the light in which we can observe the reality.

http://www.apexprofessionalsllc.org/about/http://www.apexprofessionalsllc.org/about/


February 1. 2010 04:52
buenos aires real estate
As a regular reader of your blog I'm very grateful for such a good information that you provide every time. Best Andie

http://www.buenosaireshabitat.com/http://www.buenosaireshabitat.com/


February 1. 2010 08:11
Loans in MA
When a goal matters enough to a person, that person will find a way to accomplish what at first seemed impossible.

http://superpaydayloan.com/state/Massachusettshttp://superpaydayloan.com/state/Massachusetts


February 3. 2010 03:23
CATHERINiC35
We affirm that you make really very good enough knowledge connecting with this good post. You have to write the thesis paper for <a href="www.primethesis.com">thesis</a> writing services or make our own <a href="www.primethesis.com">dissertation</a> writing services and some guys would buy dissertation online in that location.                              

http://www.primethesis.com/http://www.primethesis.com/


February 3. 2010 22:28
whiplash claims
I was just wondering would you consider hosting a guest author on this blog..cause I see you started this blog and yet you really have no relevant articles posted here...so if you are interested in allowing someone else to write posts here let me know!

http://www.whiplash-compensation.me.uk/home/http://www.whiplash-compensation.me.uk/home/


February 3. 2010 22:35
lindsey vonn pictures
I really appreciate posts, which might be of very useful for beginners in blogging as I am. I already have a small collection of blog posts and other articles, from which I step by step learn various aspects of life. Thank you for your resource.

http://www.lindseyisepic.com/http://www.lindseyisepic.com/


February 3. 2010 23:49
MMA Pound For Pound
I really liked this article. Please continue this great work

http://wongsableng.com/mma-pound-for-pound.htmlhttp://wongsableng.com/mma-pound-for-pound.html


February 8. 2010 05:30
online advance
One must learn by doing the thing. For though you think you know it, you have no certainty until you try.

http://cashusloans.com/http://cashusloans.com/


February 8. 2010 18:25
Vay mua nha
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them.

http://nganhangonline.com/dich-vu/ngan-hang-ca-nhan/vay-mua-nha-oto-laptop-8.htmlhttp://nganhangonline.com/dich-vu/ngan-hang-ca-nhan/vay-mua-nha-oto-laptop-8.html


February 8. 2010 18:40
Madison Square Garden

I really like your website because it provides many information about different programs which are really very helpful and very beneficial for us.

http://www.ticketamerica.com/madison_square_garden_tickets.htmlhttp://www.ticketamerica.com/madison_square_garden_tickets.html


February 9. 2010 02:56
cash advance payday loans
Success is neither magical nor mysterious. Success is the natural consequence of consistently applying the basic fundamentals.

http://fastloansus.com/PaydayLoans/cash-advance-payday-loans.htmlhttp://fastloansus.com/PaydayLoans/cash-advance-payday-loans.html


February 11. 2010 22:16
contemporary abstract art
I really appreciate posts, which might be of very useful for beginners in blogging as I am. I already have a small collection of blog posts and other articles, from which I step by step learn various aspects of life. Thank you for your resource.

http://www.arthaus66.com/blog/http://www.arthaus66.com/blog/


February 11. 2010 23:01
Used Conservatory
Sharepoint is a waste of energy. It's so buggy that it takes 10x longer to code with than most equivalents

http://usedconservatory.net/http://usedconservatory.net/


February 13. 2010 10:50
sexy games
Halo sorry I am from Denmark, English so so, but good update on this.

http://www.adultgames365.com/http://www.adultgames365.com/


February 14. 2010 15:52
Philips Arena
thanks for sharing ...

http://www.ticketamerica.com/philips_arena_tickets.htmlhttp://www.ticketamerica.com/philips_arena_tickets.html


February 16. 2010 00:52
webthesurfi rugs webdesign
Your website came up in my search and I'm taken by what you have composed on this topic. I am presently diversifying my research and thus cannot contribute further, even so. Just Now love it and thanks for granting my remark. Great job

http://www.fauzirohimi.com/2010/02/webthesurfi-rugs-webdesign-trend-2010.htmlhttp://www.fauzirohimi.com/2010/02/webthesurfi-rugs-webdesign-trend-2010.html

Search Blog

Blog Roll

    OPMLDownload OPML file

    Recent Comments

    Banners

    Theme Grabber
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Tony Testa's World