We have a situation where deployment of a reusable workflow for a site content type cannot be completed through the web interface due to the number of libraries where the content type is in use (time-out on deploy and update).
It was hoped that this could be accomplished with PowerShell but the method of deploying to a list content type appears to be different than it is to a list (all content types).
The below snippet works fine for a list / all content types:
function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName) {$site= Get-SPSite $SiteCollection[Guid]$wfTemplateId= New-Object Guid#Step through each web in site collection$site | Get-SPWeb -limit all | ForEach-Object {$web=$_$_.Lists | ForEach-Object{if($_.AllowContentTypes -eq$true) {if($_.ContentTypes.Item("$ctName") -ne$null) { write-host "Enabling workflow on"$_.Title "in"$_.ParentWebUrl$ct=$_.ContentTypes[$ctName]$culture= New-Object System.Globalization.CultureInfo("en-US")$template=$site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)if($template-ne$null) {$tasklist="Tasks"$historylist="Workflow History"if(!$web.Lists[$historylist]) {$web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site. It is created by the Publishing feature.", "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) { Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url }$wfHistory=$web.Lists[$historylist]$wfHistory.Hidden =$true$wfHistory.Update() }if(!$web.Lists[$tasklist]) {$web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100") }$association=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])$association.AllowManual =$true$_.AddWorkflowAssociation($association)$_.Update() }else { Write-Error "Workflow Template not found" } } } } } } AddWorkflowToLibraries <Site Name><Content Type Name><Workflow Template Name><Association Name>
However changing the association as follows causes the script to still execute without a problem but the workflow doesn't appear for the content and the associations collection is empty:
function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName) {$site= Get-SPSite $SiteCollection[Guid]$wfTemplateId= New-Object Guid#Step through each web in site collection$site | Get-SPWeb -limit all | ForEach-Object {$web=$_$_.Lists | ForEach-Object{if($_.AllowContentTypes -eq$true) {if($_.ContentTypes.Item("$ctName") -ne$null) { write-host "Enabling workflow on"$_.Title "in"$_.ParentWebUrl$ct=$_.ContentTypes[$ctName]$culture= New-Object System.Globalization.CultureInfo("en-US")$template=$site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)if($template-ne$null) {$tasklist="Tasks"$historylist="Workflow History"if(!$web.Lists[$historylist]) {$web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site. It is created by the Publishing feature.", "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) { Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url }$wfHistory=$web.Lists[$historylist]$wfHistory.Hidden =$true$wfHistory.Update() }if(!$web.Lists[$tasklist]) {$web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100") }$association=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])$association.AllowManual =$true$_.ContentTypes[$ctname].AddWorkflowAssociation($association)$_.ContentTypes[$ctname].Update() }else { Write-Error "Workflow Template not found" } } } } } } AddWorkflowToLibraries <Site Name><Content Type Name><Workflow Template Name><Association Name>
The only change is:
$association=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])$association.AllowManual =$true$_.ContentTypes[$ctname].AddWorkflowAssociation($association)$_.ContentTypes[$ctname].Update()
But unlike the list version, the association doesn't appear to be saved and no error is generated.
Is anyone aware of what may cause this or have an example in C# that may explain something my script is missing?