cancel
Showing results for 
Search instead for 
Did you mean: 

How to update Workflow Constants via PowerShell

aaron_labiosa
Nintex Newbie
20 2 3,552

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]:Smiley Very Happyeserialize([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]:Smiley Very Happyeserialize([System.IO.File]::ReadAllText("C:\export1.xml")))
2 Comments
Not applicable

Thank you for posting, Aaron. I tried this out and ran into an issue when trying to update a workflow constant stored as a credential. After trying to update the username/password of the credential, when viewing it through the Manage Workflow Constants page in Central Admin an error is thrown. The ULS logs reveal the following:

System.Xml.XmlException: Data
 at the root level is invalid. Line 1, position 1.  
 at System.Xml.XmlTextReaderImpl.Throw(Exception e)   
 at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()   
 at System.Xml.XmlTextReaderImpl.ParseDocumentContent()   
 at System.Xml.XmlReader.MoveToContent()   
 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCredentialValue.Read4_CredentialValue()

Any ideas? Thanks!

pramod_nagaraj1
Nintex Newbie

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#com... 

Thanks Both.

Cheers

Pramod