0

How to remove the workflow associations on the content type and document library using PowerShell? Currently I have added PowerShell to configure workflow in my document libraries and its associated content types. But other developers from my team also created workflow instances in the same document libraries within my sub sites and site collections. So I want to remove all those unwanted workflow associations and run my PowerShell script again with a fresh WF instance.

if($web.Lists[$tasklistName]  -ne $null -and $web.Lists[$historylistName] -
 ne $null  -and  $wfTemplate -ne $null) 
 { 
 Write-Host "task lists exists, history list exists, wftemplate exists"
 $associationWF=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]
  ::CreateListContentTypeAssociation
     ($wfTemplate, "myConfidentialWFF", $web.Lists[$tasklistName], 
      $web.Lists[$historylistName])  #$taskList,$historyList)

  $ct.WorkflowAssociations.Add($associationWF)
  $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
  Write-Host "WF association with the   content type " $ct.Name   "    
  completed for ..."  $web.url "with document library...." $singleDocLibObj
  }
  else
  {
  Write-Host "any one of the parameters or arguments is null"
  }
  } # end of  all content types within CT collection

Any methods exist similar to $ct.workflow.assocations.remove ($wfaassocaitonwf)? So that it removes all the existing instances from the document libraries and content types.

1

You can use below script for removing all Workflows Associated to a Particular List

$SPSite = Get-SPSite "http://siteurl"
$Web = $SPSite.OpenWeb();
$SPList  =$Web.Lists["ListTitle"];
$workflowAssociations = $SPList.WorkflowAssociations;
if($workflowAssociations  -ne $null)
{
    foreach($wf in $workflowAssociations)
    {
        $SPList.RemoveWorkflowAssociation($wf);
        $SPList.Update();
    }
}
  • Thanks for the immediate reply and your time. Let me try this and revert. – SaMolPP Apr 23 '15 at 14:18
  • One doubt I am having, whether the line of code $splist.removeworkflowassociations($wf) method will remove the content type association also? As per my current design each and every document library is associated with a few content types.So if i use this code what will happen to the workflow association with content types?this – SaMolPP Apr 23 '15 at 14:41
  • As per my understanding it gets removed. You can try it out. – Amal Hashim Apr 23 '15 at 14:45
  • That code didn't work. My document library is associated with content types – SaMolPP Apr 24 '15 at 5:44
  • I am not able to get that code working after running.My document libraries are associated with content types and I need to get the workflow associations for the content type and then rmeove.But I am not getting what's API for removal of content type's WF associations. – SaMolPP Apr 24 '15 at 11:51
0

You can use this script to delete all previous versions of a workflow that does not have any running instances.

This script will run on every list / library on a single Web Application, and it also targets Content Types.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
CLS
$Sites = Get-SPWebApplication http://WEBAPPLICATIONTURL | Get-SPSite -Limit all
$KillWorkflows = @()
foreach ($Site in $Sites)
{
    foreach ($Web in $Sites.allWebs)
    {
        foreach ($List in $Web.Lists)
        {
            Write-Host $List.Title -ForegroundColor Yellow -BackgroundColor Black
            #Target List Workflows
            foreach ($wf in $list.WorkFlowAssociations) 
            {
                if ($wf.Name -like "*Previous Version*" -and $wf.RunningInstances -eq 0) 
                { 
                    Write-Host $wf.Name -ForegroundColor Red -BackgroundColor Black -NoNewline
                    Write-Host $wf.RunningInstances -ForegroundColor Green -BackgroundColor Black
                    $KillWorkflows += $wf
                }
            }
            #Target Content Type Workflows
            foreach ($wf in $list.ContentTypes.WorkFlowAssociations)
            {
                if ($wf.Name -like "*Previous Version*" -and $wf.RunningInstances -eq 0) 
                { 
                    Write-Host $wf.Name -ForegroundColor Red -BackgroundColor Black -NoNewline
                    Write-Host $wf.RunningInstances -ForegroundColor Green -BackgroundColor Black
                    $KillWorkflows += $wf
                }
            }
        }
        #Delete all the Workflows
        foreach ($KillWorkflow in $KillWorkflows)
        {
            $List.RemoveWorkflowAssociation($KillWorkflow)
            $List.Update()
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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