Using PowerShell to Change SharePoint 2010 Workflow AssociationData

If you are still using SharePoint on-premises, then you will almost certainly be using workflows based on the 2010 workflow platform.  These would include all of the out-of-the-box workflow reusable templates like Approval and Collect Signatures. 

Recently I needed to change the start parameters of a workflow template that was deployed in a site template so there was a copy of the workflow in over 1,000 subsites.  Of course I turned to my trusty friend PowerShell.  At first I didn’t think this was possible because it seemed like no matter what or how I tried, I couldn’t get the settings to stick.  But I figured it out and I want to share how to do it properly.

Getting Context

In my case, the workflows were configured reusable 2010 workflows (not deployed globally).  When the workflow was added in the site template, the option for “End on First Rejection” was not enabled but we wanted it to be.  I’m referring to the options on the “Association form”:

SharePoint Workflow Association Form

These values form the defaults for the Initiation form, the one that is shown to a user that starts a workflow using the template.  There is a workflow template, a workflow association and a workflow instance.  The values and data we need to change is on workflow association.

Show Me the Script!

The first thing we need to do is get the subsite and the list, then we can enumerate through all the workflows associated to the list to find the one we need to update.

Add-PSSnapin microsoft.sharepoint.powershell
$web = get-spweb https://intranet/subsite/
$list = $web.lists["Documents"]
$wfassoc = $list.workflowassociations | ? {$_.Name -eq "Team Approval Internal Workflow"}

All of the data that we need is in the AssociationData property.

$wfassocdata = $wfassoc.AssociationData

If we dump the variable we see all of the data that we want. 

PowerShell SharePoint Workflow AssociationData

We can just do a quick text replace from $false to $true (saving the corrected text to a new variable) then we update the property with the new data and save.

$newwfassocdata = $wfassocdata.Replace("<d:CancelonRejection>false</d:CancelonRejection>","<d:CancelonRejection>true</d:CancelonRejection>")
$wfassoc.AssociationData = $newwfassocdata

$list.WorkflowAssociations.Update($wfassoc)

We call the Update() method from the WorkflowAssociations and pass in which association we want to update. 

Now if you open up the workflow settings in the browser, it will be checked!  Let’s say you wanted to instead change that the workflow starts automatically when an item is created.  Just after the $wfassoc variable, you would just run

$wfassoc.AutoStartCreate = $true

And perform the same Update as shown above.  Here is the full script for every subsite in the site collection:

Add-PSSnapin microsoft.sharepoint.powershell
$site = get-spsite “https://intranet/subsite”

foreach ($web in $site.allwebs) {
     Write-host "Checking" $web.url
     $list = $web.lists["Documents"]
     if ($list) {
         $wfassoc = $list.workflowassociations | ? {$_.Name -eq "Team Approval Internal Workflow"}
         if ($wfassoc) {
             #$wfassoc.AutoStartCreate = $true

            $wfassocdata = $wfassoc.AssociationData
             $newwfassocdata = $wfassocdata.Replace("<d:CancelonRejection>false</d:CancelonRejection>","<d:CancelonRejection>true</d:CancelonRejection>")
             $wfassoc.AssociationData = $newwfassocdata
             $list.WorkflowAssociations.Update($wfassoc)
         }
     } else {write-host "No library found, skipping" $web.url -ForegroundColor Yellow}
     $web.dispose()
}
$site.dispose()

Be sure to reach out to us if you have any questions regarding this blog, SharePoint workflows or PowerShell!