Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top