Wednesday, June 17, 2015

How to Remove and Add workflow in List programmatically?



1. Adding Workflow to the List/Library 
private void AddAssociation(SPList lst, SPWeb web)
{

web.AllowUnsafeUpdates = true;
try
{
// Get workflow template by Name
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(“SiteProvisionWorkflow”, System.Globalization.CultureInfo.CurrentCulture);

// Create workflow association
SPWorkflowAssociation workflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, “Site Provision”, web.Lists[“Tasks”], web.Lists[“Workflow History”]);

// Set workflow parameters
workflowAssociation.AllowManual = false;
workflowAssociation.AutoStartCreate = true;
workflowAssociation.AutoStartChange = false;

// Add workflow association to  list
web.Lists[“Site Provision Information”].AddWorkflowAssociation(workflowAssociation);

// Enable workflow
workflowAssociation.Enabled = true;
}
finally
{
web.AllowUnsafeUpdates = false;
}

}
2. Remove Workflow to the List/Library
private void RemoveAssociation(SPList lst, SPWeb web)
{
web.AllowUnsafeUpdates = true;
try
{
// Create workflow association
SPWorkflowAssociation workflowAssociation = web.Lists[“Site Provision Information”].WorkflowAssociations.GetAssociationByName(“Site Provision”, System.Globalization.CultureInfo.CurrentCulture);
// Remove workflow association to list
web.Lists[“Site Provision Information”].RemoveWorkflowAssociation(workflowAssociation);
}
finally
{
web.AllowUnsafeUpdates = false;
}
}

No comments:

Post a Comment