Overview

Working on a project recently, there was a requirement to export, import and publish Nintex Workflow for Office 365 between different environments. By environments, I mean SharePoint Site Collections for Testing, Staging, UAT, Prod, etc..
While it is super easy to manually export, import and publish a Nintex Workflow for Office 365, this often becomes tedious and error prone when the number of such workflows to manage increases. In the spirit of automation, I developed a simple PowerShell Script to do exactly the same.
There is a wealth of information available online regarding using Office 365 API for Workflows here.
Prerequisite
  • A source list workflow for a SharePoint Online list or library developed using Nintex Workflow for Office 365. We will export this workflow using the script.
  • A destination list workflow for a SharePoint Online list or library developed using Nintex Workflow for Office 365. We will import and publish the workflow from the step above.
  • Please contact your Nintex representative to request an API key for the REST API for use in script.

PowerShell Script

Execute the following script in your Windows PowerShell window. Please change the required variable values as per your tenant configuration settings.
  1. Function Export - NWF {
  2. Param
  3. (
  4. [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
  5. [Parameter(Mandatory = $True)][String] $WorkflowSourceURL,
  6. [Parameter(Mandatory = $True)][string] $WorkflowSourceID,
  7. [Parameter(Mandatory = $True)][string] $WorkflowExportFileNameAndPath,
  8. [Parameter(Mandatory = $True)][String] $APIBaseURL,
  9. [Parameter(Mandatory = $True)][String] $APIKey
  10. )
  11. Try {
  12. $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/$WorkflowSourceID"
  13. $creds = $Context.Credentials
  14. $AuthCookie = $creds.GetAuthenticationCookie($WorkflowSourceURL)
  15. $cookie = "cookie $WorkflowSourceURL " + $AuthCookie
  16. $HeadersExport = @ {
  17. 'Authorization' = $cookie
  18. 'Api-Key' = $APIKey 'Accept' = 'application/json'
  19. 'Content-Type' = 'application/nwp'
  20. }
  21. Invoke - RestMethod - Method "Get" - Uri $RestURL - Headers $HeadersExport - OutFile $WorkflowExportFileNameAndPath
  22. Write - host "Export-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green
  23. }
  24. Catch {
  25. write - host - f Red "Error when executing Export-NWF for $WorkflowSourceURL!"
  26. $_.Exception.Message
  27. }
  28. }
  29. Function Import - NWF {
  30. Param
  31. (
  32. [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
  33. [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,
  34. [Parameter(Mandatory = $True)][string] $WorkflowTargetID,
  35. [Parameter(Mandatory = $True)][string] $WorkflowImportFileNameAndPath,
  36. [Parameter(Mandatory = $True)][String] $APIBaseURL,
  37. [Parameter(Mandatory = $True)][String] $APIKey
  38. )
  39. Try {
  40. $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/" + $WorkflowTargetID + "?migrate=true"
  41. $creds = $Context.Credentials
  42. $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)
  43. $cookie = "cookie $WorkflowTargetURL " + $AuthCookie
  44. $HeadersImport = @ {
  45. 'Authorization' = $cookie
  46. 'Api-Key' = $APIKey 'Accept' = "application/json"
  47. }
  48. Invoke - RestMethod - Method "PUT" - Uri $RestURL - Headers $HeadersImport - InFile $WorkflowImportFileNameAndPath
  49. Write - host "Import-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green
  50. }
  51. Catch {
  52. write - host - f Red "Error when executing Import-NWF for $WorkflowTargetURL!"
  53. $_.Exception.Message
  54. }
  55. }
  56. Function Publish - NWF {
  57. Param
  58. (
  59. [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
  60. [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,
  61. [Parameter(Mandatory = $True)][string] $WorkflowTargetID,
  62. [Parameter(Mandatory = $True)][String] $APIBaseURL,
  63. [Parameter(Mandatory = $True)][String] $APIKey
  64. )
  65. Try {
  66. $creds = $Context.Credentials
  67. $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)
  68. $cookie = "cookie $WorkflowTargetURL " + $AuthCookie
  69. $HeadersImport = @ {
  70. 'Authorization' = $cookie
  71. 'Api-Key' = $APIKey 'Accept' = "application/json"
  72. }
  73. $RestURLPublish = "https://$APIBaseURL/api/v1/workflows/$WorkflowTargetID/published"
  74. Invoke - RestMethod - Method "Post" - Uri $RestURLPublish - Headers $HeadersImport
  75. Write - host "Publish-NWF for $WorkflowTargetURL executed Successfully" - ForegroundColor Green
  76. }
  77. Catch {
  78. write - host - f Red "Error when executing Publish-NWF for $WorkflowTargetURL!"
  79. $_.Exception.Message
  80. }
  81. }
  82. $APIBaseUrl = "mytenant.nintexo365.com"
  83. $APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  84. $username = '[email protected]'
  85. $password = 'xxxxxxxxxxxx'
  86. $encpassword = convertto - securestring - String $password - AsPlainText - Force
  87. $cred = new - object - typename System.Management.Automation.PSCredential - argumentlist $username, $encpassword
  88. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  89. #Export workflow from source
  90. $WorkflowSourceURL = "https://mytenant.sharepoint.com/sites/source/"
  91. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
  92. $ctx = Get - PnPContext
  93. Export - NWF - Context $ctx - WorkflowSourceURL $WorkflowSourceURL - WorkflowSourceID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" - WorkflowExportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -
  94. APIKey $APIKey
  95. #Import workflow into destination
  96. $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"
  97. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
  98. $ctx = Get - PnPContext
  99. Import - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - WorkflowImportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -
  100. APIKey $APIKey
  101. #Publish imported workflow at destination
  102. $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"
  103. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
  104. $ctx = Get - PnPContext
  105. Publish - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - APIBaseURL $APIBaseURL - APIKey $APIKey
  106. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
After successful execution of the script, check the Last modified, Latest Version Published and Modified By values for imported workflow as shown below,

Conclusion

In this article we saw how to export, import and publish Nintex Workflow for Office 365 between different environments.

I hope you liked this article! Please share your valuable comments and feedback with me.

Stay well and happy Nintexing...!