Home > SharePoint Development > How to Remove and Add workflow in List programmatically?

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;
}

}

Advertisements
  1. Nik
    13/03/2012 at 8:01 pm

    Thanks this really helped me out!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: