Deploying SPD Workflows with PowerShell

With the new SharePoint Designer creating reusable workflows has become fairly easy,
however deploying them seems to be a bit harder, lets say you create a reusable workflow
that you use globally  on all your content types, it seems it still has to be
deployed to each list you use it in. So saying you would have 50 lists with that content
type forces you to add that workflow 50 times.

For that reason the following PowerShell example might come in handy, it allows you
to search a workflow you create with SharePoint designer, and add that to your SPList
(ofcourse you can also use an SPWeb or other object that supports the SPWorkflowAssociation
to bind it to).

 

  1: #Clear old output
  2: clear;
  3:
  4: #Write some header
  5: Write-Host -ForegroundColor Green "Publish Workflow Test";
  6:
  7: #Load required dll
  8: [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Workflow")
  9:
 10: #Load Snappin
 11: $snapin="Microsoft.SharePoint.PowerShell"
 12: if (get-pssnapin
$snapin -ea "silentlycontinue") {}
 13: else {
 14:       if (get-pssnapin
$snapin -registered -ea "silentlycontinue")
{
 15:             Add-PSSnapin $snapin
 16:             write-host -f Green "PSsnapin $snapin is loaded"
 17:       }
 18:       else {
 19:             write-host -f Red "PSSnapin $snapin not found"
 20:       }
 21: }
 22:
 23: #Get some required objects
 24: $web = Get-SPWeb -Identity "http://portal.sp2010.dev";
 25: $rootweb = $web.Site.RootWeb;
 26: $wfList = $web.Lists["Pages"];
 27: $wfTaskList = $web.Lists["Workflow Tasks"];
 28: $wfHistoryList = $web.Lists["Workflow Tasks"];
 29:
 30: #Get Workflow
 31: foreach($workflow in $rootweb.WorkflowTemplates)
 32: {
 33:       #User $workflow.Name to mach on
 34:       Write-Host -ForegroundColor Yellow " Found a workflow: " $workflow.Name
 35:
 36:       if($workflow.Name
-eq "TestAppie")
 37:       {
 38:             $setflow = $workflow;
 39:       }
 40: }
 41:
 42: Write-Host -ForegroundColor Green "Found the workflow: " $setflow.Name;
 43:
 44: $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($setflow, "Test",
$wfTask, $wfHistory);
 45:
 46: $list.WorkflowAssociations.Add($association);
 47: $list.Update();
 48:
There are 2 comments for this article
    • Albert-Jan Schot at 11:44

      The basics are the same, yet instead of adding it to a list you will need to add the workflow to a Content Type. After adding the association you will need to update it using something like the following snippet:
      $ct.ContentTypes[$ctname].AddWorkflowAssociation($association);
      $ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)

Leave a Reply