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:
_________________________________________
08/26/2012 at 3:49 pm
Very helpful. But I think there is something goofy in the get-content line… close quote, close curly bracket, new line before $file?
08/27/2012 at 10:02 pm
You are right, thank you! I’ve updated the post now.
10/04/2013 at 8:26 pm
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?