none
Sharepoint Online Powershell script to run a list workflow

    Question

  • I need to run a workflow from Sharepoint online powershell to run a workflow over an entire list.

    I have seen workarounds.

    I have seen tons of scripts that work for on-premise. This is ONLINE.

    I have a script that runs all workflows. My struggle is that I want to run one list workflow not all of them and I don't find a method where I can pass the name of the workflow I want to use...  I know I can pass the GetSubscription method a GUID, but all of the methods I have for finding GUID in powershell are on premise and don't leverage the Client SDK as we need to for Sharepoint Online.

    Please help.  Here is how I have started:

    Step 1 - in my script I add the path associations at the top

    THEN
    # Specify tenant admin and site URL
    $SiteUrl = "https://mysite"
    $ListName = "xyz"
    $UserName = "uname"
    $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)
    *******************

    HELP! Once I have the items for my list, then I want to restrict to just the object specified in my $TargetWorkflow variable
    ****************************

    $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

    ***********************

    HELP, then I would like to pass, I think it needs to be GUID to the StartWorkflowOnListItem method

    *************************

        $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
        $ClientContext.ExecuteQuery()
    }

    COMMENTS:

    Thanks in advance. I will publish this corrected script once I have this working. I can see from forums this is a common issue and people have some sort of hands on and time consuming ways around it. I would like to schedule this powershell script in Scheduler and have it run my workflow nightly.

    Monday, March 20, 2017 4:06 PM

All replies

  • Hi GwenMHP,
     
    From your description, there is a workaround to schedule workflow in SharePoint Online: add a retention policy.

    Define a information management policy for a list, library or content type that already has a workflow associated with it. And add retention stage that action select "start a workflow".

    Reference:

    https://support.office.com/en-us/article/Create-and-apply-information-management-policies-EB501FE9-2EF6-4150-945A-65A6451EE9E9

    Best regards,

    Grace Wang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 21, 2017 3:57 PM