How to update Workflow Constants via PowerShell

  • 13 January 2015
  • 1 reply
  • 89 views

Userlevel 7
Badge +10

Products: Nintex Workflow 2013, Nintex Workflow 2010

 

We received a request the other day to bulk update Workflow Constants. While it is easy to import-export Workflow constants, it is not a trivial matter to update a great many of them. Typically updating involves logging into Central Administration and/or each site and updating the credentials manually.

 

Here is a method that takes some of the work out of updating these values by allowing you to edit an exported Workflow constants file using a specific format and persist these changes back into the environment in an automated fashion.

 

Things you will need:

 

  1. An export of Workflow Constants in file format.
  2. Some kind of XML Viewer (Notepad++ is what I prefer).
  3. A copy of this script (below and attached).

 

Steps:

 

  1. Export the Workflow Constants you want updated and save them to a file (More information here: NWAdmin Operations - Nintex Workflow 2013)
  2. Open the File in an XML Viewer. This should present you with one long line.
  3. Save a copy of this file in case something goes wrong and you want to revert back to the original values.
  4. Replace all '><' instances with a line break/carriage return to make working with the XML file easier (Notepad++ example below).2015-01-13 12_11_15-_new  8 - Notepad++.png
  5. You should end up with something that looks like this: 2015-01-13 12_14_33-_new  8 - Notepad++.png
  6. Edit each WorkflowConstant's Value node to have a UserName and Password as the new value with a space separating the two values (Important Note: You must have a space separating the username and password to avoid parsing issues):           2015-01-13 12_16_57-_new  8 - Notepad++.png
  7. Any Workflow Constants you do not wish to update should be removed from this file.
  8. Once you have updated the workflow constants file, save it and update the following reference in the PowerShell script (Line 71) to the location and filename chosen: Execute([Nintex.Workflow.WorkflowConstantCollection]::Deserialize([System.IO.File]::ReadAllText("C:export1.xml")))

  9. Run the script and perform and testing to ensure the values are what they should be.

     

 

PowerShell Script
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  2. [void][System.Reflection.Assembly]::LoadWithPartialName("Nintex.Workflow")
  3. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Serialization")
  4. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Text")
  5. [void][System.Reflection.Assembly]::LoadWithPartialName("System.IO")
  6.  
  7. function ProcessCredential([Nintex.Workflow.WorkflowConstant]$constant){
  8.  
  9. $credentialValue = New-Object -TypeName "Nintex.Workflow.CredentialValue"
  10. $stringBuilder = New-Object -TypeName "System.Text.StringBuilder"
  11.  
  12. $serializer = [System.Xml.Serialization.XmlSerializer]($credentialValue.GetType())
  13.  
  14. $stringWriter = [System.IO.StringWriter]($stringBuilder)
  15.  
  16. $credentialValue.Password = $constant.Value.Split("{ }")[1]
  17. $credentialValue.Username = $constant.Value.Split("{ }")[0]
  18.  
  19. $serializer.Serialize($stringWriter, $credentialValue)
  20.  
  21. $constant.Value = $stringBuilder.ToString()
  22.  
  23. #$constant.EncryptValueField()
  24.  
  25. return $constant
  26. }
  27.  
  28. function ProcessUsageSecurity([Nintex.Workflow.WorkflowConstant]$constant){
  29.  
  30. if ($constant.UsageSecurity -ne $null){
  31.  
  32. $constant.UsageSecurity.Update()
  33.  
  34. }
  35.  
  36. }
  37.  
  38. function Execute([Nintex.Workflow.WorkflowConstantCollection]$collection){
  39.  
  40. foreach($constant in $collection){
  41.  
  42. $existingWorkflowConstantId = [Nintex.Workflow.WorkflowConstantCollection]::GetWorkflowConstantIdByName($constant.Title, $constant.WebId,$constant.SiteId)
  43.  
  44. [Nintex.Workflow.WorkflowConstant]$c = ProcessCredential($constant)
  45.  
  46. if($existingWorkflowConstantId -eq -1){
  47.  
  48. $newConstant = New-Object "Nintex.Workflow.WorkflowConstant" ($c.Title, $c.Description, $c.Value, $c.Sensitive, $c.SiteId, $c.WebId, $c.Type,$c.AdminOnly)
  49.  
  50. $newConstant.Id = -1
  51.  
  52. Write-Host "Adding $($constant.Title) to Workflow Constants."
  53.  
  54. $newConstant.Update()
  55.  
  56. }
  57. else{
  58. $newConstant = New-Object "Nintex.Workflow.WorkflowConstant" ($c.Title, $c.Description, $c.Value, $c.Sensitive, $c.SiteId, $c.WebId, $c.Type,$c.AdminOnly)
  59. $newConstant.Id = $existingWorkflowConstantId
  60.  
  61. Write-Host "Updating $($constant.Title) in Workflow Constants."
  62.  
  63. $newConstant.Update()
  64. }
  65.  
  66. ProcessUsageSecurity($c)
  67.  
  68. }
  69.  
  70. }
  71.  
  72. Execute([Nintex.Workflow.WorkflowConstantCollection]::Deserialize([System.IO.File]::ReadAllText("C:export1.xml")))
 

1 reply

Badge +3

Thanks Aaron this was very useful especially the part to check if the constant already exist

I used your script in combination with Martin Harris's script in https://sharepointrepairjoint.blogspot.com.au/2014/07/powershell-cmdlet-to-provision-nintex.html#comment-form 

Thanks Both.

Cheers

Pramod

Reply