SharePoint Online: Get Workflow Inventory using PowerShell

Requirement: Find all workflows in a SharePoint Online site collection and export the workflow inventory to CSV report.
PowerShell to Get Workflow Inventory in SharePoint Online

PowerShell to Export Workflow Inventory in SharePoint Online:
This scripts gets all workflows from all lists and libraries from a given SharePoint Online site collection and export the workflow data into a CSV file.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

#Function to Get workflows in a site
Function Get-SPOWorkflowInventory($SiteURL, $CSVPath)
{
    Try{
        $WorkflowInventory = @()
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get the Web and its Subsites
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Lists = $Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()

        #Initialize Workflow Manager Object
        $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)

        Write-host -f Yellow "Searching Workflows in Site: $SiteURL"
        #Loop through each list and get all workflows sharepoint online powershell
        ForEach($List in $Lists)
        {
            #Get SharePoint 2013 Workflows Associated with the List    
            $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
            $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
            $Ctx.Load($WorkflowAssociations)
            $Ctx.ExecuteQuery()

            #Loop through each workflow associated with the List
            Foreach ($Association in $WorkflowAssociations | Where {$_.Name -notlike "*Previous Version*"})
            { 
                $WorkflowData = New-Object PSObject
                $WorkflowData | Add-Member NoteProperty WorkflowName($Association.Name)
                $WorkflowData | Add-Member NoteProperty SiteURL($Web.Url)
                $WorkflowData | Add-Member NoteProperty ListName($List.Title)
                Write-host -f Green "`t Found Workflow '$($Association.Name)' in list '$($List.Title)'"
                $WorkflowInventory+=$WorkflowData
            }

            #Get SharePoint 2010 Workflows Associated
            $WorkflowAssociations = $List.WorkflowAssociations
            $Ctx.Load($WorkflowAssociations)
            $Ctx.ExecuteQuery()
            ForEach($Association in $WorkflowAssociations | Where {$_.Name -notlike "*Previous Version*"})
            {
                $WorkflowData = New-Object PSObject
                $WorkflowData | Add-Member NoteProperty WorkflowName($Association.Name)
                $WorkflowData | Add-Member NoteProperty SiteURL($Web.Url)
                $WorkflowData | Add-Member NoteProperty ListName($List.Title)
                Write-host -f Green "`t Found Workflow '$($Association.Name)' in list '$($List.Title)'"
                $WorkflowInventory+=$WorkflowData
            }
        }
        #Export Workflow data to CSV File
        If($WorkflowInventory) { $WorkflowInventory | Export-CSV -LiteralPath $CSVPath -NoTypeInformation -Append}

        #Process Subsites
        Foreach($Subweb in $Web.Webs)
        {
            Get-SPOWorkflowInventory -SiteURL $Subweb.url
        }
    }
    Catch {
    Write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL="https://crescenttech.sharepoint.com"
$CSVPath = "C:\Temp\WorkflowInventory.csv"

#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to get workflow inventory
Get-SPOWorkflowInventory $SiteURL $CSVPath

Get All Running Workflow Instances on a SharePoint Online List
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

#Set Parameters
$SiteURL="https://crescenttech.sharepoint.com"
$ListName ="Project Tasks"

#Get Credentials to connect
$Cred= Get-Credential

Try{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
    #Get the Web, List and List Item Objects
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $Ctx.Load($List)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Initialize Workflow Manager and other related objects
    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
    $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() 
    $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
    $Ctx.Load($WorkflowAssociations)
    $Ctx.ExecuteQuery()

    #Loop through each List Item
    ForEach($ListItem in $ListItems)
    {
        #Get Workflow Instances of the List Item            
        $WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id) 
        $Ctx.Load($WorkflowInstanceCollection)
        $Ctx.ExecuteQuery()
        
        #Get all Workflow Instances in progress
        ForEach ($WorkflowInstance in $WorkflowInstanceCollection | Where {$_.Status -eq "Started"}) 
        { 
            [PSCustomObject] @{
                                ItemID              = $ListItem.ID
                                Status              = $WorkflowInstance.Status
                                WorkflowStarted     = $WorkflowInstance.InstanceCreated
                                WorkflowAssociation = $WorkflowAssociations | where {$_.ID -eq $WorkflowInstance.WorkflowSubscriptionId} | Select -ExpandProperty Name
                                ItemUrl = "$($Web.Context.Web.Url)/$($workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.CurrentItemUrl"])"
                                StartedBy = $workflowInstance.Properties["Microsoft.SharePoint.ActivationProperties.InitiatorUserId"]
            }
        }
    }
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}

2 comments:

  1. Hi, Get All Running Workflow Instances on a SharePoint Online List for SP 2010 Workflows and on diffrent Content types?

    ReplyDelete
  2. An awesome site for PowerShell!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.