Wednesday, April 07, 2010

Workflows P2

So, I was working on a project and need to programatically fire off a work flow for a list item. Easy you say? Well it is. Sort of. See first I needed to know WHICH work flow to fire off (in case there were multiple workflows associated with the list). So here is the code I used:

private void GetWorkflows()
{
SPWeb web = new SPSite "http://serverurl").OpenWeb();
SPList list = web.Lists["list name"];

string strWorkFlows = "";

//This would normally be passed in. Its the List Item ID
string strTaskID = "4";

foreach (SPWorkflowAssociation wfa in list.WorkflowAssociations)
{
if (wfa.BaseTemplate.Name == "NameOfWorkflow")
{
strWorkFlows += wfa.InstantiationUrl + "?List=" + list.ID.ToString() + "&ID=" + strTaskID + "&TemplateID={" + wfa.ParentAssociationId.ToString() + "}&Source=" + list.DefaultViewUrl;
}
}

Neat huh? What I did was re-create the URL that exists when you initiate a workflow from the list itself. The 'BaseTemplate.Name' will give you the name of the workflow assigned in the feature, not the one assigned by whoever created the association. This is helpful for making sure you find the correct workflow. That's it. Ping me with any questions/thoughts/suggestions.

No comments: