0

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

enter image description here

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()

1 Answer 1

0

Contacted Microsoft support and eventually they located 1800 orphaned workflows and deleted them. I asked them if I had the access to do this in future but they said no and provided the following response:

"These orphaned workflows are not within SharePoint side. It’s orphaned within backend instance. It’s non customer facing part.

It’s related to the activities lead to orphaned workflows generated. for example, run scripts to create multiple duplicated workflows then deletion, publish workflows then delete lists, etc. "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.