Change the Approvers Group in a Workflow with PowerShell

I recently received a request where someone needed a script that could programmatically switch the approvers group set in a workflow to a newly created SharePoint group. They wanted this to be scripted so they could run this multiple times, if necessary.

I thought to myself, “Well this should be easy, I’m sure the Approvers group is a property stored within the workflow association.” Close, but not so close. It is stored in the workflow association for the list in which the workflow is attached, but it’s stored within an XML formatted property called AssociationData.

In order for us to switch the approvers group we need to retrieve this property, replace the group with our desired group, then set the property back. You will notice that I include “>” and “<” around the SharePoint Group that we are setting it to. This is because there are other places within the Association Data property that uses the word “Approvers” that does not directly relate to the default Approvers group set on the workflow that we are trying to replace. The script starts with several variables for the web, the list, the workflow and groups. The association collection then has to be updated at the end of the script to apply the changes to the workflow.

Script:

$outLoc = "c:\workflowdata.txt"
$web = Get-SPWeb -Identity "http://portal.company.com/subsite"
$list=$web.Lists["Pages"]
$associationColl=$list.WorkflowAssociations
$workflowName = "Page Approval"
$defaultApprovers = "Approvers"
$newApprovers = "New Approvers"

foreach ($association in $associationColl)
{
  If ($association.Name -eq $workflowName){
     $pageApproval = $association
     $pageApproval.AssociationData | Out-File $outLoc
     (Get-Content $outLoc) | Foreach-Object {$_ -replace ">$defaultApprovers<", ">$newApprovers<"} | Set-Content $outLoc
     $file = Get-Content $outLoc
     $pageApproval.AssociationData = $file
  }
}

$associationColl.Update($pageApproval)
Remove-Item $outLoc -recurse
$web.Dispose()

Before running the script:

After running the script:

_________________________________________

Advertisements

Subscribe

Subscribe to our RSS feed and social profiles to receive updates.

3 Comments on “Change the Approvers Group in a Workflow with PowerShell”

  1. Paul Says:

    Very helpful. But I think there is something goofy in the get-content line… close quote, close curly bracket, new line before $file?

    Reply

  2. Kashish Sukhija Says:

    Hi Adam, I am having same issue but in my case associated data is empty as approvers are different for evey list item and captured during workflow initiation. Do you see if there can be same solution applied to Initiation Data?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: