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.
- Function Export - NWF {
- Param
- (
- [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
-
- [Parameter(Mandatory = $True)][String] $WorkflowSourceURL,
- [Parameter(Mandatory = $True)][string] $WorkflowSourceID,
- [Parameter(Mandatory = $True)][string] $WorkflowExportFileNameAndPath,
- [Parameter(Mandatory = $True)][String] $APIBaseURL,
- [Parameter(Mandatory = $True)][String] $APIKey
- )
-
- Try {
-
- $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/$WorkflowSourceID"
-
- $creds = $Context.Credentials
- $AuthCookie = $creds.GetAuthenticationCookie($WorkflowSourceURL)
- $cookie = "cookie $WorkflowSourceURL " + $AuthCookie
-
- $HeadersExport = @ {
- 'Authorization' = $cookie
- 'Api-Key' = $APIKey 'Accept' = 'application/json'
- 'Content-Type' = 'application/nwp'
- }
-
- Invoke - RestMethod - Method "Get" - Uri $RestURL - Headers $HeadersExport - OutFile $WorkflowExportFileNameAndPath
-
-
- Write - host "Export-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green
- }
-
- Catch {
- write - host - f Red "Error when executing Export-NWF for $WorkflowSourceURL!"
- $_.Exception.Message
- }
-
- }
-
- Function Import - NWF {
- Param
- (
- [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
- [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,
- [Parameter(Mandatory = $True)][string] $WorkflowTargetID,
- [Parameter(Mandatory = $True)][string] $WorkflowImportFileNameAndPath,
- [Parameter(Mandatory = $True)][String] $APIBaseURL,
- [Parameter(Mandatory = $True)][String] $APIKey
- )
-
- Try {
-
- $RestURL = "https://$APIBaseURL/api/v1/workflows/packages/" + $WorkflowTargetID + "?migrate=true"
-
- $creds = $Context.Credentials
- $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)
- $cookie = "cookie $WorkflowTargetURL " + $AuthCookie
-
- $HeadersImport = @ {
- 'Authorization' = $cookie
- 'Api-Key' = $APIKey 'Accept' = "application/json"
- }
-
- Invoke - RestMethod - Method "PUT" - Uri $RestURL - Headers $HeadersImport - InFile $WorkflowImportFileNameAndPath
-
- Write - host "Import-NWF for $WorkflowSourceURL executed Successfully" - ForegroundColor Green
- }
-
- Catch {
- write - host - f Red "Error when executing Import-NWF for $WorkflowTargetURL!"
- $_.Exception.Message
- }
-
- }
-
- Function Publish - NWF {
- Param
- (
- [Parameter(Mandatory = $True)][Microsoft.SharePoint.Client.ClientContext][ValidateNotNullOrEmpty()] $Context,
- [Parameter(Mandatory = $True)][String] $WorkflowTargetURL,
- [Parameter(Mandatory = $True)][string] $WorkflowTargetID,
- [Parameter(Mandatory = $True)][String] $APIBaseURL,
- [Parameter(Mandatory = $True)][String] $APIKey
- )
-
- Try {
-
- $creds = $Context.Credentials
- $AuthCookie = $creds.GetAuthenticationCookie($WorkflowTargetURL)
- $cookie = "cookie $WorkflowTargetURL " + $AuthCookie
-
- $HeadersImport = @ {
- 'Authorization' = $cookie
- 'Api-Key' = $APIKey 'Accept' = "application/json"
- }
-
- $RestURLPublish = "https://$APIBaseURL/api/v1/workflows/$WorkflowTargetID/published"
-
- Invoke - RestMethod - Method "Post" - Uri $RestURLPublish - Headers $HeadersImport
-
- Write - host "Publish-NWF for $WorkflowTargetURL executed Successfully" - ForegroundColor Green
- }
-
- Catch {
- write - host - f Red "Error when executing Publish-NWF for $WorkflowTargetURL!"
- $_.Exception.Message
- }
-
- }
-
- $APIBaseUrl = "mytenant.nintexo365.com"
- $APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-
- $username = '[email protected]'
- $password = 'xxxxxxxxxxxx'
-
- $encpassword = convertto - securestring - String $password - AsPlainText - Force
- $cred = new - object - typename System.Management.Automation.PSCredential - argumentlist $username, $encpassword
-
- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
-
- #Export workflow from source
-
- $WorkflowSourceURL = "https://mytenant.sharepoint.com/sites/source/"
-
- Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
-
- $ctx = Get - PnPContext
-
- Export - NWF - Context $ctx - WorkflowSourceURL $WorkflowSourceURL - WorkflowSourceID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" - WorkflowExportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -
- APIKey $APIKey
-
- #Import workflow into destination
-
- $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"
-
- Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
-
- $ctx = Get - PnPContext
-
- Import - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - WorkflowImportFileNameAndPath "c:\Test.nwp" - APIBaseURL $APIBaseURL -
- APIKey $APIKey
-
- #Publish imported workflow at destination
-
- $WorkflowTargetURL = "https://mytenant.sharepoint.com/sites/destination/"
-
- Connect - PnPOnline - Url $WorkflowSourceURL - Credentials $cred
-
- $ctx = Get - PnPContext
-
- Publish - NWF - Context $ctx - WorkflowTargetURL $WorkflowTargetURL - WorkflowTargetID "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" - APIBaseURL $APIBaseURL - APIKey $APIKey
- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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...!