Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Could you please point me to some good articles/links that explains how to remove a Workflow from the list using SP Object Model?

I am not having much luck with Google today!

Thanks a lot for looking into it.

share|improve this question
Well, you can work backwards from get-spscripts.com/2010/08/… the interesting API phrase is SPList.WorkflowAssociations :-) – user166390 Oct 28 '11 at 17:58

2 Answers

up vote 1 down vote accepted

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();
}
share|improve this answer

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

share|improve this answer
This removes the workflow association(s) but the workflow still shows as inactive workflow in the workflow settings. How would you completely remove from the site? – MrMVCMan Sep 11 '12 at 16:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.