How to create new instance of SharePoint 2007 workflow through C# code

 In SharePoint 2007, very good feature I like much is that framework
supports for writing custom events, features, workflows or any other stuff... We
can customize complete SharePoint system at any context.
While working with
SharePoint custom workflows, we get some scenarios where we need to stop a
workflow or start a new instance of workflow through coding. So, I did some
research on the MSDN library and saw all the dll's by doing some reflection on
the libraries and found some functions and classes which supports
this.

Here, I want to present a small code snippet which does terminating
currently running wokflow and start a new instance of the workflow on the same
list item.

  1. SPListItem listItem = workflowProperties.Item;
     

  2. SPList spList = workflowProperties.List;  

  3. string initData = workflowProperties.InitiationData;
     

  4.  

  5. const string WF_GUID = "The GUID of workflow template";
     

  6. workflowProperties.Site.WorkflowManager.RemoveWorkflowFromListItem(workflowProperties.Workflow);
     

  7. workflowProperties.Site.WorkflowManager.Dispose();
     

  8.  

  9. SPWorkflowAssociation associationTemplate= spList.WorkflowAssociations.GetAssociationByBaseID(new Guid(WF_GUID));  

  10.  

  11. workflowProperties.Site.WorkflowManager.StartWorkflow(listItem, associationTemplate, initData); 

Hope this will give you a good start to code on SharePoint workflow start and
terminate through coding. Let me know, what you think.