Created
October 2, 2015 03:12
-
-
Save aaira-a/c839340476f0cf2ee552 to your computer and use it in GitHub Desktop.
loop start a site workflow on sharepoint online using powershell csom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM | |
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” | |
#Specify tenant admin and site URL | |
$SiteUrl = "mysiteurl" | |
$UserName = "myusername" | |
$SecurePassword = ConvertTo-SecureString "mypassword" -AsPlainText -Force | |
#Bind to site collection | |
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) | |
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword) | |
$ClientContext.Credentials = $credentials | |
$ClientContext.ExecuteQuery() | |
# Create WorkflowServicesManager instance | |
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web) | |
# Connect to WorkflowSubscriptionService | |
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService() | |
# Connect WorkflowInstanceService instance | |
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() | |
$ClientContext.Load($WorkflowServicesManager) | |
$ClientContext.Load($WorkflowSubscriptionService) | |
$ClientContext.Load($WorkflowInstanceService) | |
$ClientContext.ExecuteQuery() | |
# Get Site Workflows | |
$SiteWorkflows = $WorkflowSubscriptionService.EnumerateSubscriptions() | |
$ClientContext.Load($SiteWorkflows) | |
$ClientContext.ExecuteQuery() | |
$SiteWorkflows | Select Id, Name, StatusFieldName | FT -AutoSize | |
# Define Site Workflow | |
$SiteWorkflowID = 'mysiteworkflowid' | |
$SiteWorkflow = $WorkflowSubscriptionService.GetSubscription($SiteWorkflowID) | |
$ClientContext.Load($SiteWorkflow) | |
$ClientContext.ExecuteQuery() | |
# Prepare Start Workflow Payload | |
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]' | |
# Loop Start Workflow | |
For ($j=0; $j -lt 10; $j++){ | |
$Action = $WorkflowInstanceService.StartWorkflow($SiteWorkflow, $Dict) | |
$ClientContext.ExecuteQuery() | |
# Get Workflow Status | |
$WorkflowInstance = $Action.Value | |
$WorkflowStatus = $WorkflowInstanceService.GetInstance($Action.Value) | |
$ClientContext.Load($WorkflowStatus) | |
$ClientContext.ExecuteQuery() | |
Write-Host $Action.Value $WorkflowStatus.Status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment