Monday, July 22, 2024

Script to export the running workflow instances into an Excel file

# Load SharePoint snap-in if not already loaded

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) {

    Add-PSSnapin Microsoft.SharePoint.PowerShell

}


# Define the site URL, list name, and item ID

$siteUrl = "http://cssvc.corning.com/nafta/otc"

$listName = "OrderRelease"

$itemId = 55695  # Specify the ID of the item whose workflows you want to cancel


# Get the site, web, and list objects

#$site = Get-SPweb $siteUrl

$web = Get-SPweb $siteUrl

$list = $web.Lists[$listName]


# Get the list item

$item = $list.GetItemById($itemId)


# Get the workflow manager

$workflowManager = $web.Site.WorkflowManager


# Get all running workflows for the list item

$workflows = $workflowManager.GetItemActiveWorkflows($item)


# Cancel running workflows

foreach ($workflow in $workflows) {

    if ($workflow.InternalState -eq [Microsoft.SharePoint.Workflow.SPWorkflowState]::Running) {

        Write-Host "Cancelling workflow instance ID: $($workflow.InstanceId) for item ID: $itemId"

        #$workflowManager.RemoveWorkflow($workflow)

    }

}


# Clean up

$web.Dispose()

#$site.Dispose()


Write-Host "All running workflows for item ID $itemId have been cancelled."