Forum Discussion

Jason H's avatar
Jason H
Copper Contributor
Dec 11, 2019

Cannot publish workflow to scope because it already contains the maximum number of workflows allowed

I'm getting the attached error (IDs hidden) when I'm running Powershell CSOM to delete existing List workflows and create a new workflow from a template.  Googled the error but it no results returned

 

Can anyone help?

 

Error:

Exception calling "ExecuteQuery" with "0" argument(s): "Microsoft.Workflow.Client.InvalidRequestException: Cannot publish workflow <ID> to scope
/spo/<ID>/<ID>/<ID> because it already contains the maximum number of workflows allowed in a scope (1800). HTTP headers received from the
server - ActivityId: <ID>. NodeId: WFFE_IN_0. Scope: /spo/<ID>/<ID>"

 

Script details ($Context, $ListName, $WorkflowName and $XamlPath are passed into the function):

$List = $Context.Web.Lists.GetByTitle($ListName)

    $Context.Load($List)
    $web = $Context.Web
    $Context.Load($web)
    $Context.ExecuteQuery()

    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Context, $Context.Web)
    $Context.Load($WorkflowServicesManager)
    $Context.ExecuteQuery()

    # Get WorkflowAssociations with List
    $wfDeploymentService = $WorkflowServicesManager.GetWorkflowDeploymentService()
    $Context.Load($wfDeploymentService)
    $Context.ExecuteQuery()

    ###### CLEANUP #######
    # Removes the old workflow
    try{
        $wfDefinitions = $wfDeploymentService.EnumerateDefinitions($false)
        $Context.Load($wfDefinitions)
        $Context.ExecuteQuery()
        $ExistingReportDefinitions = $wfDefinitions | where-object {$_.DisplayName -like $WorkflowName}
        if ($ExistingReportDefinitions) {
            foreach ($ExistingReport in $ExistingReportDefinitions) {                
                $wfDeploymentService.DeleteDefinition($ExistingReport.id)
                Write-Host "Deleted workflow $WorkflowName"
            }
            $Context.Load($wfDeploymentService)
            $Context.ExecuteQuery()
        }
    }catch{
        Write-Host "No pre-existing workflow.  Continuing with workflow creation" -ForegroundColor Yellow
    }

    $Xaml = [System.Xml.Linq.XElement]::Load($XamlPath)

    # Prepare the Workflow Definition object
    $wfDefinition = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowDefinition -ArgumentList $Context
    $wfDefinition.DisplayName = $WorkflowName
    $wfDefinition.Description = "List Workflow $WorkflowName associated with $ListName"
    $wfDefinition.Xaml = $Xaml.ToString()

    # Save and publish the Workflow Definition object
    $definitionId = $wfDeploymentService.SaveDefinition($wfDefinition)
    $Context.Load($wfDefinition)
    $Context.ExecuteQuery()
    
    # Publish the Workflow Definition
    $wfDeploymentService.PublishDefinition($definitionId.Value)

    $historyList = $Context.Web.Lists.GetByTitle("Workflow History")
    $Context.Load($historyList)
    $tasksList = $Context.Web.Lists.GetByTitle("Workflow Tasks")
    $Context.Load($tasksList)
    $Context.ExecuteQuery()

    # Associate the Workflow Definition to a target list/library
    $WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
    $Context.Load($WorkflowSubscriptionService)
    $Context.ExecuteQuery()
    $wfSubscription = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowSubscription -ArgumentList $Context

    # Configure the Workflow Subscription 
    $wfSubscription.DefinitionId = $definitionId.Value
    $wfSubscription.Name = $wfDefinition.DisplayName
    $wfSubscription.Enabled = $true
    $eventTypes = New-Object System.Collections.Generic.List[String]

    # Available values are: ItemAdded, ItemUpdated, WorkflowStart
    $eventTypes.Add("WorkflowStart")
    #$eventTypes.Add("ItemAdded")
    $eventTypes.Add("ItemUpdated")
    $wfSubscription.EventTypes = $eventTypes
    
    $wfSubscription.EventSourceId = $List.Id.guid.ToString()
    $wfSubscription.SetProperty("TaskListId", $tasksList.Id.ToString())
    $wfSubscription.SetProperty("HistoryListId", $historyList.Id.ToString())

    # Publish the Workflow Subscription
    $WorkflowSubscriptionService.PublishSubscriptionForList($wfSubscription, $List.Id.guid.ToString()) | Out-Null
    $Context.ExecuteQuery()

No RepliesBe the first to reply

Resources