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.
SPListItem listItem = workflowProperties.Item; SPList spList = workflowProperties.List; string initData = workflowProperties.InitiationData; const string WF_GUID = “The GUID of workflow template”; workflowProperties.Site.WorkflowManager.RemoveWorkflowFromListItem(workflowProperties.Workflow); workflowProperties.Site.WorkflowManager.Dispose(); SPWorkflowAssociation associationTemplate= spList.WorkflowAssociations.GetAssociationByBaseID(new Guid(WF_GUID)); 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.