Cancel multiple SharePoint Workflows using PowerShell
Asked Answered
A

4

6

How is it possible to cancel all running workflows in a SharePoint (2010) List?

I found this script via technet.

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/d3913265-9712-4e61-9e38-1f9b78c8f718/

CODE:

using (SPSite oSite = new SPSite("<your url>"))
{
    foreach (SPWeb oWeb in oSite.AllWebs)
    {
        oWeb.AllowUnsafeUpdates = true;

        // stop list workflows
        foreach (SPList list in oWeb.Lists)
        {
            foreach (SPListItem oItem in list.Items)
            {
                foreach (SPWorkflow workflow in oItem.Workflows)
                {
                    SPWorkflowManager.CancelWorkflow(workflow);
                }
            }
        }

        // stop site workflows
        foreach (SPWorkflow workflow in oWeb.Workflows)
        {
            SPWorkflowManager.CancelWorkflow(workflow);
        }

        oWeb.AllowUnsafeUpdates = false;
        oWeb.Dispose();
    }
}

Many thanks for your help.

Acquiescence answered 29/9, 2011 at 9:31 Comment(0)
T
12

Give this one a shot.

I modified it so that it is done on a specific list on a specific web.

#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;    

#List Name
$list = $web.Lists["ListName"];

# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows        
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
$web.Dispose();

Worked just fine for me. Let me know if it works for you.

Tender answered 13/10, 2011 at 18:50 Comment(0)
N
3

This script has been wickedly useful to me. I make a modification to allow me to cancel all the workflows in one list or just one workflow and thought I would post it here too:

    #Parameters
    param($listToCancel,$WfToCancel)

    #Site URL 
    $web = Get-SPWeb "http://mydomain.com"; 
    $web.AllowUnsafeUpdates = $true;     

    #List Name 
    $list = $web.Lists[$listToCancel]; 

    #Add wildcards to Wf variable
    $WildcardWfToCancel = "*"+$WfToCancel+"*";

    # Iterate through all Items in List and all Workflows on Items.          
    foreach ($item in $list.Items) { 
    foreach ($wf in $item.Workflows) { 

    #Test for workflow complete and match criteria
    if (($wf.ParentAssociation.InternalName -like $WildcardWfToCancel) -and ($wf.IsCompleted  -ne $true))        {

    #Show status and cancel Workflows
    write-Host $wf.ItemName -nonewline;
    write-host "     " -nonewline;
    write-host $wf.ParentAssociation.InternalName;
    Write-Host " Status " -nonewline;
    Write-host $wf.InternalState;     

    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);       
    }
    } 
    } 
    $web.Dispose(); 
Narcotize answered 5/12, 2011 at 18:44 Comment(0)
C
0

Try this (simple "translation" of C# to powershell):

$site = Get-SPSite "<your url>";

$site.AllWebs | foreach {
  $web = $_;
  $web.AllowUnsafeUpdates = $true;

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  # stop site workflows
  $web.Workflows | foreach {
    $wf = $_;
    [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
  }    

  $web.AllowUnsafeUpdates = $false;
  $web.Dispose();
}

$site.Dispose();
Carrion answered 29/9, 2011 at 17:10 Comment(2)
Hey Pedro, many thanks for your help! Your answer was very useful to me. I changed the script to only run through subwebs. The Script already works canceling the workflows, but there is one problem left. I get an "Enumeration error". How do I resolve this. I found out that this is an array problem while changing the SharePoint-Web.Acquiescence
can you post the powershell "stack trace" and / or the modified version of the script ? Are you sure the user that is running that powershell script has privileges to enumerate and access all the webs or your SPSite ?Carrion
A
0

here is my modified script:

$web = Get-SPWeb "http://site/subsite";

  $web.allowUnsafeUpdates = 'true';

  # stop list workflows
  $web.Lists | foreach {
    $list = $_;
    $list.Items | foreach {
      $item = $_;
      $item.Workflows | foreach {
         $wf = $_;
         [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }
    }
  }

  $web.allowUnsafeUpdates = 'false';
  $web.Dispose();

an this is the error i get (i am using a german localized version of the server and sharepoint):

Fehler bei der Enumeration einer Auflistung: Collection was modified; enumeration operation may not execute..
Bei C:\stopwf.ps1:7 Zeichen:3
+    <<<< $web.Lists | foreach {
    + CategoryInfo          : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
    + FullyQualifiedErrorId : BadEnumeration

the user runnig the script is admin on the machine it self, sitecollection admin and sql-admin. i also created the subsite with this account, so i don't think that useracces would be the problem.

I found this answer (Differences Between PowerShell and C# when Enumerating a Collection) but i don't get a clue how to use this information for me?!

Acquiescence answered 6/10, 2011 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.