How To Programmatically Trigger a Custom Workflow

Triggering SharePoint workflows programmatically from within a custom built Visual Studio workflow is not difficult once you know where to look. Tony Testa wrote a great post on this very same thing that describes these concepts in more detail than I will — so go check that out if you haven’t already. I had to tweak his concept a little bit because I was doing this within an already running workflow that belonged to a content type not a list.

So in my scenario, I had a single workflow tied to a Content Type, and because I knew there was only one workflow, I used index at 0 to get the current workflow association. If your workflow is tied to a list, then you may not need to call ContentType, you might be able to use:

1
SPWorkflowAssociation workflowAssociation = workflowProperties.List.WorkflowAssocations[0];

If you have multiple workflows tied to a Content Type or a List, you will need to grab the Guid associationId for that workflow instead of using the index.

Finally, once I have my workflow association configured, I can start my workflow by passing it the parameters it needs:

1
2
3
4
5
if (workflowProperties.Site.WorkflowManager.GetItemActiveWorkflows(setLeaderItem).Count == 0)
{
    SPWorkflowAssociation workflowAssociation = workflowProperties.Item.ContentType.WorkflowAssociations[0];
    workflowProperties.Site.WorkflowManager.StartWorkflow(setLeaderItem, workflowAssociation, workflowAssociation.AssociationData, true);
}

The IF block is there to check if any active workflow are running on that list item. If there aren’t, it attempts to start the workflow. There you have it! That should trigger the workflow on the list item you specified.

,

2 Responses to “How To Programmatically Trigger a Custom Workflow”

  1. fasttoshiba July 25, 2009 at 1:31 pm #

    Hi…I am new user to sharepoint and workflow designs………Can you please explain me step to step how to create basic workflow and how to Programmatically start a Custom Workflow ………..I am little confused what you are doing…………

Trackbacks/Pingbacks

  1. Links (7/9/2009) « Steve Pietrek – Everything SharePoint - July 9, 2009

    […] How To Programmatically Trigger a Custom Workflow […]

Leave a Reply