Guest User

Untitled

a guest
Jun 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. # Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
  2. Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll")
  3. Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll")
  4. Add-Type -Path (Resolve-Path "$env:CommonProgramFilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.WorkflowServices.dll")
  5.  
  6. # Specify tenant admin and site URL
  7. $SiteUrl = Read-Host -Prompt "Site url."
  8. $ListName = Read-Host -Prompt "List name"
  9. $UserName = Read-Host -Prompt "User Name"
  10. $SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString
  11.  
  12. # Connect to site
  13. $ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
  14. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
  15. $ClientContext.Credentials = $credentials
  16. $ClientContext.ExecuteQuery()
  17.  
  18. # Get List and List Items
  19. $List = $ClientContext.Web.Lists.GetByTitle($ListName)
  20. $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
  21. $ClientContext.Load($List)
  22. $ClientContext.Load($ListItems)
  23. $ClientContext.ExecuteQuery()
  24.  
  25. # Retrieve WorkflowService related objects
  26. $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
  27. $WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
  28. $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
  29. $ClientContext.Load($WorkflowServicesManager)
  30. $ClientContext.Load($WorkflowSubscriptionService)
  31. $ClientContext.Load($WorkflowInstanceService)
  32. $ClientContext.ExecuteQuery()
  33. # Get WorkflowAssociations with List
  34. $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
  35. $ClientContext.Load($WorkflowAssociations)
  36. $ClientContext.ExecuteQuery()
  37.  
  38. # Prepare Start Workflow Payload
  39. $Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
  40.  
  41. # Loop List Items to Start Workflow
  42. For ($j=0; $j -lt $ListItems.Count; $j++){
  43. $msg = [string]::Format("Starting workflow {0}, on ListItemId {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
  44. Write-Host $msg
  45. #Start Workflow on List Item
  46. $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
  47. $ClientContext.ExecuteQuery()
  48. }
Add Comment
Please, Sign In to add comment