The workflow I have deployed/enabled globally is a SharePoint 2013 workflow.
SharePoint 2013 workflows are hosted on the Workflow Manager platform.
To add the workflow on all Libraries, we need to add a new workflow subscription to the workflow manager using the ID of our Workflow definition.
To find the workflow definition ID:
$Web = Get-SPWeb http://site/web/ #Web where the Workflow feature is enabled. $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web) $defSevice = $wfm.GetWorkflowDeploymentService() $wfDefs = $defSevice.EnumerateDefinitions($false) $wfDefs | Format-Table ID, DisplayName
Set the workflow definition ID from the above result into the $WorkflowDefinition variable below.
$Web = Get-SPWeb http://intranet/web/ $Web.webs | ForEach-Object { $_.webs | ForEach-Object { #I'm targeting 3rd Web down. $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($_) $defSevice = $wfm.GetWorkflowDeploymentService() $wfDefs = $defSevice.EnumerateDefinitions($false) ### Update below variable with yours $WorkflowDefinition = "Workflow Definition ID goes here" ### $wfDef = $wfDefs | where {$_.Id -eq $WorkflowDefinition} $wfTaskList = $_.Lists["Workflow Tasks"]; #Task and History lists must exist, see note below. $wfHistoryList = $_.Lists["Workflow History"] $_.lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object { if($_.title -eq "Site Assets") { write-host "Skiping Site Assets" } else { $wfSubService = $wfm.GetWorkflowSubscriptionService() #Create Workflow Subscription $sub = New-object Microsoft.SharePoint.WorkflowServices.WorkflowSubscription $sub.DefinitionId = $WorkflowDefinition $sub.Enabled = $true $sub.Name = $wfDef.DisplayName #Build start options $startOptions = New-Object "System.Collections.ObjectModel.Collection[System.String]" # $startOptions.Add("ItemAdded") #When item added # $startOptions.Add("ItemUpdated") #When item updated $startOptions.Add("WorkflowStart") #Allow manual start $sub.EventTypes = $startOptions $sub.SetProperty("HistoryListId", $wfHistoryList.Id) $sub.SetProperty("TaskListId", $wfTaskList.Id) try{ $wfSubService.PublishSubscriptionForList($sub, $_.Id); write-host "Workflow" $wfDef.DisplayName "successfully associated to" $_.title } catch{ write-host "Workflow FAILED to associate for" $_.title } } #End Else } #End Lists Loop } #End $_.webs Loop } #End $Web.webs Loop
The task/history lists must exist in each library before running the above code.
Enabling the Publishing feature on a site creates these for you. You can do this through PowerShell here. (refer to bottom of post)
Resources:
– StackExchange question answered by Caroline which is where I got the basis of the above code from.
– Great post on Get-SPScripts which shows adding a workflow through PowerShell and seems to add the Tasks/History list if they don’t exist.