SharePoint Online (O365): PowerShell Script to get Workflow status

SharePoint Online supports only fewer PowerShell commands to do the tasks.  There are about 30 commands only available.  Please refer to the following article for list of commands.

Introduction to the SharePoint Online Management Shell

One of the daunting task is find the workflow status for each item in the list for the particular site especially in SharePoint Online due to limited PowerShell Commands.

To resolve this issue, we are going to use the Client Side Object Model (CSOM) in PowerShell.  We will be using Client Context to connect to the SharePoint Online site.

Step 1

Add the references before using CSOM objects. The following libraries will be available on the client machine once you install SharePoint Online Management Shell.

Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”

Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll”

Step 2

Connect to the SharePoint Online site.

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)

$clientContext.Credentials = $credentials

 Step 3

Connect to to Workflow Service Manager to get the Subscription service and Workflow instances.

$workflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($clientContext, $web);

$workflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService();

$workflowInstanceSevice = $workflowServicesManager.GetWorkflowInstanceService();

 Step 4

Iterate through all lists and find out whether any workflow associated with the list.  If any workflow associated with list, workflow status will be displayed for each work item.

foreach($listItem in $listItems)
{

$workflowInstanceCollection = $workflowInstanceSevice.EnumerateInstancesForListItem($list.Id, $listItem.Id);
$clientContext.Load($workflowInstanceCollection);
$clientContext.ExecuteQuery();
foreach ($workflowInstance in $workflowInstanceCollection)
{

Write-Host “List Item Title:”$listItem[“Title”]
Write-Host “Workflow Status:”$workflowInstance.Status
Write-Host “Last Updated:”$workflowInstance.LastUpdated
Write-Host “”

}

}

Final output will be as per the following screenshot.

WfStatus

For complete script, please download from the technet gallery.  Please rate the gallery if you like it.

Workflow Status for List Items – SharePoint Online

This entry was posted in SharePoint 2013. Bookmark the permalink.

One Response to SharePoint Online (O365): PowerShell Script to get Workflow status

  1. Yogesh says:

    Nice Article Mokhtar. Happy Coding!!

Leave a Reply