Monday, April 27, 2009

Creating new instance of SharePoint 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.
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.

1 comment:

  1. Great post! exactly what I was looking for.

    Thx!

    ReplyDelete