Remove duplicate Workflows displayed on the existing sites

Let us consider the following scenario.

  • You have a Microsoft Office SharePoint Server 2007 site that includes a document library. This document library contains a document for which the Approval workflow and the Collect Feedback workflow are created.
  • You save the site as a template.
  • You create a new site that uses the saved template.

In this scenario, two instances of the Approval workflow and of the Collect Feedback workflow are displayed in the document library on the new site.

A workaround to this problem is available at https://support.microsoft.com/kb/951654.  But, what about the sites created earlier as described in the scenario.  How to remove OOB duplicate workflows for existing sites.  You can either remove the duplicate workflows from SharePoint UI, “Document library” > Settings > Workflow settings > Remove Workflows or programmatically remove them.  The advantage of the later method is that it can loop through entire farm and fix the problem for all sites in a single shot.

Here is the sample code to remove duplicate workflows at the web application level.

 SPFarm thisFarm = SPFarm.Local; 
 SPWebService service = thisFarm.Services.GetValue<SPWebService>("");
 SPWebApplication spWebApp = service.WebApplications["WebApp"];// Name of the web application
 SPSiteCollection siteColl = spWebApp.Sites;
 foreach (SPSite site in siteColl)
 {
     SPWebCollection webColl = site.AllWebs;
     foreach (SPWeb web in webColl)
     {
         try
         {
             SPList spList = web.Lists["Shared Documents"];//Document Library Name
             SPWorkflowAssociationCollection spWFAssociation = spList.ContentTypes["Document"].WorkflowAssociations;
             List<string> aListDuplicates = new List<string>();
             int associationCount = spWFAssociation.Count;
             for (int i = associationCount - 1; i >= 0; i--)
             {
                 if ((spWFAssociation[i].InternalName == "Approval" && !(aListDuplicates.Contains(spWFAssociation[i].InternalName))) ||
                               (spWFAssociation[i].InternalName == "Collect Feedback" && !(aListDuplicates.Contains(spWFAssociation[i].InternalName))))
                 {
                     aListDuplicates.Add(spWFAssociation[i].InternalName);
                     spList.ContentTypes["Document"].RemoveWorkflowAssociation(spWFAssociation[i]);
                     spList.Update();
                 }
  
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine("Error occured :" + ex.StackTrace.ToString());           
         }
  
     }
 }

Also, after removing the duplicate workflows, when we run any OOB workflow ( like Approval, Collect Feedback) or custom workflow, we will see this error message “The workflow failed to start due to an internal error”.  The reason being "Office SharePoint Server Workflow Task" content type is missing.  In order to create this content type the OffWFCommon FEATURE needs to be activated.  This can be done using the following command:

stsadm -o activatefeature -url https://server/sites/SiteName -force -name OffWFCommon