Deploying SPD Workflows with PowerShell

Deploying SPD Workflows with PowerShell header image

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(of course you can also use an SPWeb or other object that supports the SPWorkflowAssociation to bind it to).

#Clear old output
clear;
#Write some header
Write-Host -ForegroundColor Green "Publish Workflow Test";
#Load required dll
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Workflow")
#Load Snappin
$snapin="Microsoft.SharePoint.PowerShell"
if (get-pssnapin $snapin -ea "silentlycontinue") {}
else {
  if (get-pssnapin $snapin -registered -ea "silentlycontinue") {
    Add-PSSnapin $snapin
    write-host -f Green "PSsnapin $snapin is loaded"
  }
  else {
    write-host -f Red "PSSnapin $snapin not found"
  }
}

#Get some required objects
$web = Get-SPWeb -Identity "http://portal.sp2010.dev";
$rootweb = $web.Site.RootWeb;
$wfList = $web.Lists["Pages"];
$wfTaskList = $web.Lists["Workflow Tasks"];
$wfHistoryList = $web.Lists["Workflow Tasks"];

#Get Workflow
foreach($workflow in $rootweb.WorkflowTemplates)
{
  #User $workflow.Name to mach on
  Write-Host -ForegroundColor Yellow " Found a workflow: " $workflow.Name
  if($workflow.Name -eq "TestAppie"){
    $setflow = $workflow;
  }
}
Write-Host -ForegroundColor Green "Found the workflow: " $setflow.Name;
$association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]eateListContentTypeAssociation($setflow, "Test", $wfTask, $wfHistory);
$list.WorkflowAssociations.Add($association);
$list.Update();
Loading comments…