How to update Workflow Constants via PowerShell

Blog Post created by aaron.labiosa@nintex.com Champion on Jan 13, 2015

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).




  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. function ProcessCredential([Nintex.Workflow.WorkflowConstant]$constant){
  7. $credentialValue = New-Object -TypeName "Nintex.Workflow.CredentialValue"
  8. $stringBuilder = New-Object -TypeName "System.Text.StringBuilder"
  9. $serializer = [System.Xml.Serialization.XmlSerializer]($credentialValue.GetType())
  10. $stringWriter = [System.IO.StringWriter]($stringBuilder)
  11. $credentialValue.Password = $constant.Value.Split("{ }")[1]
  12. $credentialValue.Username = $constant.Value.Split("{ }")[0]
  13. $serializer.Serialize($stringWriter, $credentialValue)
  14. $constant.Value = $stringBuilder.ToString()
  15. #$constant.EncryptValueField()
  16. return $constant
  17. }
  18. function ProcessUsageSecurity([Nintex.Workflow.WorkflowConstant]$constant){
  19. if ($constant.UsageSecurity -ne $null){
  20. $constant.UsageSecurity.Update()
  21. }
  22. }
  23. function Execute([Nintex.Workflow.WorkflowConstantCollection]$collection){
  24. foreach($constant in $collection){
  25. $existingWorkflowConstantId = [Nintex.Workflow.WorkflowConstantCollection]::GetWorkflowConstantIdByName($constant.Title, $constant.WebId,$constant.SiteId)
  26. [Nintex.Workflow.WorkflowConstant]$c = ProcessCredential($constant)
  27. if($existingWorkflowConstantId -eq -1){
  28. $newConstant = New-Object "Nintex.Workflow.WorkflowConstant" ($c.Title, $c.Description, $c.Value, $c.Sensitive, $c.SiteId, $c.WebId, $c.Type,$c.AdminOnly)
  29. $newConstant.Id = -1
  30. Write-Host "Adding $($constant.Title) to Workflow Constants."
  31. $newConstant.Update()
  32. }
  33. else{
  34. $newConstant = New-Object "Nintex.Workflow.WorkflowConstant" ($c.Title, $c.Description, $c.Value, $c.Sensitive, $c.SiteId, $c.WebId, $c.Type,$c.AdminOnly)
  35. $newConstant.Id = $existingWorkflowConstantId
  36. Write-Host "Updating $($constant.Title) in Workflow Constants."
  37. $newConstant.Update()
  38. }
  39. ProcessUsageSecurity($c)
  40. }
  41. }
  42. Execute([Nintex.Workflow.WorkflowConstantCollection]::Deserialize([System.IO.File]::ReadAllText("C:\export1.xml")))