SharePoint Online: Start Workflow on All Items in a List using PowerShell
Requirement: Run Workflow in SharePoint Online using PowerShell.
SharePoint Online: PowerShell to Start Workflow
Workflows in SharePoint Online help organizations automate business processes and increase productivity. If you need to start a workflow on all items in a list – It can be a time-consuming and tedious process. PowerShell can be used to start a workflow for all items in a SharePoint Online list. This article will describe how to start a SharePoint Online workflow for all items in a list using PowerShell.
This PowerShell script starts a given workflow on all items in 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://Crescent.sharepoint.com/Sites/PMO"
$ListName ="Project Tasks"
$WorkflowName ="Send Approval Email"
#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 Items Object
$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()
#Get the Target workflow to Run
$WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
#Start Workflow on each List Item
ForEach($ListItem in $ListItems)
{
#sharepoint online powershell start workflow
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
$Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociation, $ListItem.Id, $Dict)
}
$Ctx.ExecuteQuery()
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
This PowerShell runs workflow in each item of a list in SharePoint Online.
PnP PowerShell to Start Workflow in SharePoint Online
This time, let’s run a specific SharePoint Online workflow using PowerShell on a particular item. Here is how to trigger SharePoint Online workflow using PnP PowerShell:
#Parameters
$SiteUrl = "https://crescent.sharepoint.com/sites/ICPapers"
$ListName = "IC Docs"
$WorkflowName = "Set Document Permissions"
$ItemID = 23
#Connect to SharePoint Online
Connect-PnPOnline $SiteUrl -Interactive
#Get the Workflow to Run
$WFSubscription = Get-PnPWorkflowSubscription -List $ListName -Name $WorkflowName
#Start Workflow
Start-PnPWorkflowInstance -Subscription $WFSubscription -ListItem $ItemID
In summary, starting a workflow for all items in a SharePoint Online list using PowerShell is a simple and efficient process that can help you automate your business processes and improve productivity. By using the script provided in this guide, you can quickly start a workflow for all items in a list without the need for manual intervention.
Hello Salaudeen,
If there is a PnP to start a workflow, How about to end it?