I need create a PowerShell code to get the status of a workflow in a list items of the SharePoint Online. It's simple in On Premise SharePoint, but in online I have serious difficulties.
Someone can help me?
You could utilize CSOM/REST API for that purpose, below is demonstrated how to consume CSOM to retrieve workflow status in PowerShell.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
if([string]::IsNullOrEmpty($Password)) {
$SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
}
else {
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
}
return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}
Function GetWorkflowStatus([Microsoft.SharePoint.Client.List]$list,[int]$listItemId, [string]$workflowName)
{
$context = $list.Context
$listItem = $list.GetItemById($listItemId)
$context.Load($listItem.FieldValuesAsHtml)
$workflowStatusField = $listItem.ParentList.Fields.GetByTitle($workflowName)
$context.Load($workflowStatusField)
$context.ExecuteQuery()
if ($listItem.FieldValuesAsHtml[$workflowStatusField.StaticName] -ne $null)
{
$statusValue = $listItem.FieldValuesAsHtml[$workflowStatusField.StaticName]
return $statusValue;
}
return $null
}
$UserName = "[email protected]"
$Password = Read-Host -Prompt "Enter the password"
$WebUrl = "https://tenant.sharepoint.com/"
$ListTitle = "Documents"
$ListItemId = 1
$WorkflowTitle = "Approval"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
$list = $Context.Web.Lists.GetByTitle($ListTitle)
$statusValue = GetWorkflowStatus -list $list -listItemId $ListItemId -workflowName $workflowTitle
$Context.Dispose()
Key points:
GetWorkflowStatus
demonstrates how to retrieve workflow
status nameSharePoint Online Management Shell is very limited to use. As you can see in the below article only few options are available to query Sharepoint online. Unfortunately there is no cmdlets for getting workflow status or something.