Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
- Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll")
- Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll")
- Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.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()
- }
Add Comment
Please, Sign In to add comment