1

I have a lot of objects in a list, and have built a workflow to set a few parameters to the objects, now I'd like to run this workflow on all objects, instead of running it separately on every single object. Can someone help me out?

1

2 Answers 2

1

Workflows are attached to a list-item only (or a site) and will be automatically triggered if updates/adds happen (If you set these options)

In order to fire a workflow for all items in a list you could create a site workflow and loop over each item in the list.

Another option is to use CSOM and start a workflow for each item in the list by code. this can be done via workflowInstanceService.StartWorkflowOnListItem while looping over all items in the list.

0
1

If you have access to Powershell you can accomplish this.

    # Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")

# Specify tenant admin and site URL
$SiteUrl = Read-Host -Prompt "Site url."
$ListName = Read-Host -Prompt "List name"
$UserName = Read-Host -Prompt "User Name"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString 

# Connect to site
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

# Get List and List Items
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($List)
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()

# Retrieve WorkflowService related objects
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()
# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()

# Prepare Start Workflow Payload
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

# Loop List Items to Start Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){
    $msg = [string]::Format("Starting workflow {0}, on ListItemId {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
    Write-Host $msg
    #Start Workflow on List Item
    $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
    $ClientContext.ExecuteQuery()
}

I have used this a number of times. If you have multiple workflows on this list that can be run on demand disable the ones you don't want run. Then run the script. Good luck.

2
  • Thanks, might be to advanced for what I'm trying to do, but I'll check with colleagues.
    – Robert
    Jun 21, 2018 at 9:59
  • That's the only way to do it properly. The only thing to consider is that this script executes only the 1st workflow it finds. So make sure its the right one if you have multiple ones assigned to the list Jun 26, 2018 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.