Removing Workflow from the list using SP Object Model in SharePoint 2010

2.3k views Asked by At

I am interested in removing a Workflow from the list using the SP Object Model. How can I do this?

I am not having much luck with Google today!

2

There are 2 answers

0
Moon On BEST ANSWER

OK. So here is the function I wrote that removes the Workflow from the list. Hope it helps someone :)


/// <summary>
/// Removes the workflow.
/// </summary>
/// <param name="workflowName">Name of the workflow.</param>
/// <param name="spList">The sp list.</param>
private static void RemoveWorkflow(string workflowName, SPList spList)
{
    SPWorkflowAssociation spWorkflowAssociation =
        spList.WorkflowAssociations.Cast<SPWorkflowAssociation>()
          .FirstOrDefault(workflowAssociation => workflowAssociation.Name.Equals(workflowName));

    if (spWorkflowAssociation != null)
    {
        spList.WorkflowAssociations.Remove(spWorkflowAssociation.Id);
    }

    spList.Update();
}
1
SharePoint Build On

Try this code,

   using(SPSite oSite = new SPSite("http://localhost/"))
   {
      using(SPWeb oWeb = oSite.OpenWeb())
      {
        SPList oList = oWeb.Lists["DocumentLib"];
        SPWorkflowAssociation objWorkflowAssociation = oList.WorkflowAssociations.Cast<SPWorkflowAssociation>().FirstOrDefault(workflowAssociation => workflowAssociation.Name.Equals("Approval Workflow"));
        if (objWorkflowAssociation != null)
        {
            oList.WorkflowAssociations.Remove(objWorkflowAssociation.Id);
        }
        oList.Update();
      }
   }

Its working on my end...