Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel multiple SharePoint Workflows using PowerShell

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.

like image 437
DaviideSnow Avatar asked Sep 29 '11 09:09

DaviideSnow


People also ask

How do I delete a workflow list in SharePoint?

Locate "Workflows" in the folder list and select the (+) sign next to it. Locate the workflow that you want to delete from the server and click so it is highlighted in blue. Click the "Delete" button and confirm your selection by clicking "Yes."

How do I delete a workflow history in SharePoint?

Navigate to the Manage workflow history lists settings. Select the SharePoint workflow history list to purge items from. In the Ribbon, click on the Purge items button.

Are SharePoint workflows going away?

SharePoint 2010 workflows are deprecated but will remain supported for the SharePoint Server Subscription Edition release until July 14, 2026.


2 Answers

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.

like image 86
Justin Brink Avatar answered Oct 05 '22 23:10

Justin Brink


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(); 
like image 41
steam23 Avatar answered Oct 05 '22 23:10

steam23