Code Snippet to remove duplicate workflow associations from a content type

 
    1:              using (SPSite site = new SPSite("https://ms9:105"))
    2:              {
    3:                  using (SPWeb web = site.OpenWeb())
    4:                  {
    5:                      SPList list = web.Lists["Shared Documents"];
    6:                      
    7:                      foreach(SPContentType con in list.ContentTypes)
    8:                      {
    9:                          if (con.Name == "Document")
   10:                          {
   11:                              SPWorkflowAssociationCollection assocCol = con.WorkflowAssociations;
   12:                              bool App = false;
   13:                              bool Feed = false;
   14:                              bool Sig = false;
   15:                              for (int i = 0; i < assocCol.Count; i++)
   16:                              {
   17:                                  if (assocCol[i].Name == "Approval" && !App)
   18:                                  {
   19:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   20:                                      App = true;
   21:                                  }
   22:                                  if (assocCol[i].Name == "Collect Feedback" && !Feed)
   23:                                  {
   24:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   25:                                      Feed = true;
   26:                                  }
   27:                                  if (assocCol[i].Name == "Collect Signatures" && !Sig)
   28:                                  {
   29:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   30:                                      Sig = true;
   31:                                  }
   32:                              }
   33:                          }
   34:                      }
   35:                  }
   36:              }
   37:   
   38:              MessageBox.Show("Done!");

Above snippet looks for the specific workflow name but if we want to remove based on the workflow type then the code would be like following.

 

    1:  using (SPSite site = new SPSite("https://ms10:500/sites/test"))
    2:              {
    3:                  using (SPWeb web = site.OpenWeb())
    4:                  {
    5:                      SPList list = web.Lists["Shared Documents"];
    6:                      
    7:                      foreach(SPContentType con in list.ContentTypes)
    8:                      {
    9:                          if (con.Name == "Document")
   10:                          {
   11:                              SPWorkflowAssociationCollection assocCol = con.WorkflowAssociations;
   12:                              SPWorkflowAssociation wrkflow = null;
   13:                              bool App = false;
   14:                              bool Feed = false;
   15:                              bool Sig = false;
   16:                              bool flag = false;
   17:                              for (int i = 0; i < assocCol.Count; i++)
   18:                              {
   19:                                  flag = false;
   20:                                  if (assocCol[i].BaseTemplate.Name == "Approval" && !App)
   21:                                  {
   22:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   23:                                      App = true;
   24:                                      flag = true;
   25:                                  }
   26:                                  if (assocCol[i].BaseTemplate.Name == "Collect Feedback" && !Feed)
   27:                                  {
   28:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   29:                                      Feed = true;
   30:                                      flag = true;
   31:                                  }
   32:                                  if (assocCol[i].BaseTemplate.Name == "Collect Signatures" && !Sig)
   33:                                  {
   34:                                      list.RemoveWorkflowAssociation(assocCol[i]);
   35:                                      Sig = true;
   36:                                      flag = true;
   37:                                  }
   38:   
   39:                                  if (!flag)
   40:                                  {
   41:                                      wrkflow = con.WorkflowAssociations[i];
   42:                                      wrkflow.Enabled = true;
   43:                                      list.UpdateWorkflowAssociation(wrkflow);
   44:                                      wrkflow = null;
   45:                                  }
   46:                              }
   47:                          }
   48:                      }
   49:                  }
   50:              }
   51:   
   52:              MessageBox.Show("Done!");

 

image

:: Please note that I would have just uploaded my initial code and you might want to consider proper optimization of the code and disposal of objects properly. I might not have updated the latest code here.