Thursday, February 2, 2012

Sharepoint 2010 - Associate Workflow to List with Power Shell

I create a new Workflow Template (globally reusable) in root site and I associate it to a List in a Site Template.
But I need to update each sub site (created from my Site Template) with the new Workflow.

I use PowerShell, to cycle each subsite and associate WF to content type list.
This is command I used:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site=Get-SPSite "http://sharepoint_root_site"

foreach($web in $site.AllWebs){
   $list=$web.Lists["MyList"]
   $taskList=$web.Lists["Workflow Tasks"]
   $historyList=$web.Lists["Workflow History"]
   $ct=$list.ContentTypes["MyContenType"]
   $culture=New-Object System.Globalization.CultureInfo("en-US")
   $wfTemplate=$web.WorkflowTemplates.GetTemplateByName("wfTemplate", $culture)
   $associationWF=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($wfTemplate, "MyWFInSubSite", $taskList,$historyList)
   $ct.WorkflowAssociations.Add($associationWF)
   $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
}

It helps me for massive update.