Export, Import and Publish Nintex Workflow for O365 using REST API

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.   
  6.             [Parameter(Mandatory = $True)][String] $WorkflowSourceURL,  
  7.             [Parameter(Mandatory = $True)][string] $WorkflowSourceID,  
  8.             [Parameter(Mandatory = $True)][string] $WorkflowExportFileNameAndPath,  
  9.             [Parameter(Mandatory = $True)][String] $APIBaseURL,  
  10.             [Parameter(Mandatory = $True)][String] $APIKey  
  11.         )  
  12.   
  13.     Try {  
  14.   
  15.         $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/$WorkflowSourceID"  
  16.   
  17.         $creds = $Context.Credentials  
  18.         $AuthCookie = $creds.GetAuthenticationCookie($WorkflowSourceURL)  
  19.         $cookie = "cookie $WorkflowSourceURL " + $AuthCookie  
  20.   
  21.         $HeadersExport = @ {  
  22.             'Authorization' = $cookie  
  23.                 'Api-Key' = $APIKey 'Accept' = 'application/json'  
  24.             'Content-Type' = 'application/nwp'  
  25.         }  
  26.   
  27.         Invoke - RestMethod - Method "Get" - Uri $RestURL - Headers $HeadersExport - OutFile $WorkflowExportFileNameAndPath  
  28.   
  29.   
  30.         Write - host "Export-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green  
  31.     }  
  32.   
  33.     Catch {  
  34.         write - host - f Red "Error when executing Export-NWF for $WorkflowSourceURL!"  
  35.         $_.Exception.Message  
  36.     }  
  37.   
  38. }  
  39.   
  40. Function Import - NWF {  
  41.     Param  
  42.         (  
  43.             [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,  
  44.             [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,  
  45.             [Parameter(Mandatory = $True)][string] $WorkflowTargetID,  
  46.             [Parameter(Mandatory = $True)][string] $WorkflowImportFileNameAndPath,  
  47.             [Parameter(Mandatory = $True)][String] $APIBaseURL,  
  48.             [Parameter(Mandatory = $True)][String] $APIKey  
  49.         )  
  50.   
  51.     Try {  
  52.   
  53.         $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/" + $WorkflowTargetID + "?migrate=true"  
  54.   
  55.         $creds = $Context.Credentials  
  56.         $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)  
  57.         $cookie = "cookie $WorkflowTargetURL " + $AuthCookie  
  58.   
  59.         $HeadersImport = @ {  
  60.             'Authorization' = $cookie  
  61.                 'Api-Key' = $APIKey 'Accept' = "application/json"  
  62.         }  
  63.   
  64.         Invoke - RestMethod - Method "PUT" - Uri $RestURL - Headers $HeadersImport - InFile $WorkflowImportFileNameAndPath  
  65.   
  66.         Write - host "Import-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green  
  67.     }  
  68.   
  69.     Catch {  
  70.         write - host - f Red "Error when executing Import-NWF for $WorkflowTargetURL!"  
  71.         $_.Exception.Message  
  72.     }  
  73.   
  74. }  
  75.   
  76. Function Publish - NWF {  
  77.     Param  
  78.         (  
  79.             [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,  
  80.             [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,  
  81.             [Parameter(Mandatory = $True)][string] $WorkflowTargetID,  
  82.             [Parameter(Mandatory = $True)][String] $APIBaseURL,  
  83.             [Parameter(Mandatory = $True)][String] $APIKey  
  84.         )  
  85.   
  86.     Try {  
  87.   
  88.         $creds = $Context.Credentials  
  89.         $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)  
  90.         $cookie = "cookie $WorkflowTargetURL " + $AuthCookie  
  91.   
  92.         $HeadersImport = @ {  
  93.             'Authorization' = $cookie  
  94.                 'Api-Key' = $APIKey 'Accept' = "application/json"  
  95.         }  
  96.   
  97.         $RestURLPublish = "https://$APIBaseURL/api/v1/workflows/$WorkflowTargetID/published"  
  98.   
  99.         Invoke - RestMethod - Method "Post" - Uri $RestURLPublish - Headers $HeadersImport  
  100.   
  101.         Write - host "Publish-NWF for $WorkflowTargetURL executed Successfully" - ForegroundColor Green  
  102.     }  
  103.   
  104.     Catch {  
  105.         write - host - f Red "Error when executing Publish-NWF for $WorkflowTargetURL!"  
  106.         $_.Exception.Message  
  107.     }  
  108.   
  109. }  
  110.   
  111. $APIBaseUrl = "mytenant.nintexo365.com"  
  112. $APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  
  113.   
  114. $username = '[email protected]'  
  115. $password = 'xxxxxxxxxxxx'  
  116.   
  117. $encpassword = convertto - securestring - String $password - AsPlainText - Force  
  118. $cred = new - object - typename System.Management.Automation.PSCredential - argumentlist $username, $encpassword  
  119.  
  120. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
  121.  
  122. #Export workflow from source  
  123.   
  124. $WorkflowSourceURL = "https://mytenant.sharepoint.com/sites/source/"  
  125.   
  126. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred  
  127.   
  128. $ctx = Get - PnPContext  
  129.   
  130. Export - NWF - Context $ctx - WorkflowSourceURL $WorkflowSourceURL - WorkflowSourceID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" - WorkflowExportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -  
  131.     APIKey $APIKey  
  132.  
  133. #Import workflow into destination  
  134.   
  135. $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"  
  136.   
  137. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred  
  138.   
  139. $ctx = Get - PnPContext  
  140.   
  141. Import - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - WorkflowImportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -  
  142.     APIKey $APIKey  
  143.  
  144. #Publish imported workflow at destination  
  145.   
  146. $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"  
  147.   
  148. Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred  
  149.   
  150. $ctx = Get - PnPContext  
  151.   
  152. Publish - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - APIBaseURL $APIBaseURL - APIKey $APIKey  
  153. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
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...!