Thursday, March 17, 2011

Import and Publish Nintex Workflows programmatically

I recently developed a SharePoint solution which requires that 3 Nintex 2010 workflows are published on 13 document libraries in 22 SharePoint sites. I developed the Nintex workflows on a single library in one site and gave it to the test team to perform quality assurance. Now that I know the workflows are working well, we are ready to export them and upload and publish them to a much larger test environment. This will require that we import and publish 3 workflows x 13 libraries x 22 sites …thus a repetitive 858 manual tasks… there is no way that we will do this manually, so I took a little time to develop a tool which will programmatically import and publish workflows.
I found that it really is easy to utilise the Nintex web services to manage workflows.
In this tutorial I will give you a high level overview of how to use code to upload / import .nwf files to a target SharePoint site thereby automating the process as much as possible. This approach will help you to move workflows between development-, test- and production environments.
What we want to achieve:
I developed a simple tool which allows you to select one or more exported workflow files as source and then specify the target environment to which the workflows must be imported and published.
image



You need the following:
1. A few working Nintex workflows exported to local .nwf files.
2. A target SharePoint site with document libraries or lists which you can use as target to import and publish the workflows to.
3. Nintex Web Services must be accessible (installed with Nintex).
Preparation:
1. .NWF Files: To create a few workflows to test with I created a few really simple workflows like the one below which just update a single field on the list / library. I then saved the workflow, published it and tested it. After I have confirmation that the workflow works well I proceeded to export the workflow as a local .nwf file. Remember that this must be done in a SharePoint site which is different than your target site or you have to delete the workflows from the site before you test the import code.
image





2. Target SharePoint site: Your target SharePoint site must have Nintex Workflow installed and configured. You can go to your test target library and from the ribbon verify that you have access to create or manage Nintex workflows:
image


3. Nintex Web Services: You should be able to browse to the Nintex Workflow web services.
These web services are included as part of the Nintex Workflow out of the box installation.
The URL will be <SP Web App URL></_vti_bin/nintexworkflow/workflow.asmx>
Example: My target site is in the following web application http://ltp-21:17819/ so the URL which I will use to access the Nintex Workflow web services will be:
http://ltp-21:17819/_vti_bin/nintexworkflow/workflow.asmx
You should be able to use Internet Explorer to navigate to the web service URL.

Let’s Write the Code:
Please note that in order to simplify the tutorial I will only show the code which is required to import and publish the workflows.
I therefore assume that you will be able to develop the additional code which you will need to make a tool similar to the screenshot at the beginning of this post fully functional. If you are interested in any other bits of the code for the complete tool please leave a comment and I will gladly send it to you.
1. Create a new Visual Studio Windows Forms Application.
2. Because we target SharePoint 2010 remember to set your platform target to 64 bit:
image
3. Add a web service reference to the Nintex Workflow web service:
Remember the URL of the Nintex Workflow web service is in the form
<SP Web App URL></_vti_bin/nintexworkflow/workflow.asmx>
Example: My target site is in the following web application http://ltp-21:17819/ so the URL which I will use to access the Nintex Workflow web services will be:
http://ltp-21:17819/_vti_bin/nintexworkflow/workflow.asmx
I used NintexWorkflowService as the web reference name
image
4. Add a new button named cmdImportWorkflow onto Form1:
image
5. Add the following code to the button click event:

try
{
NintexWorkflowService.NintexWorkflowWS objWFService = new NintexWorkflowService.NintexWorkflowWS();
objWFService.Url = "http://ltp-21:17819/MyNintexSite/_vti_bin/nintexworkflow/workflow.asmx";
objWFService.Credentials = System.Net.CredentialCache.DefaultCredentials;
byte[] arrWorkflowFile = System.IO.File.ReadAllBytes(@"E:\WorkflowsToImport\UpdateTitlewithTime.nwf");
objWFService.PublishFromNWF(arrWorkflowFile, "MyDocs", "My Imported Workflow", true);
}
catch (Exception exception)
{
MessageBox.Show("An error occured while trying to publish the workflows: " + exception.Message);
}
MessageBox.Show("Done");
Let’s Analyse the code:
1. Instantiate the Nintex Workflows web method:
NintexWorkflowService.NintexWorkflowWS objWFService = new NintexWorkflowService.NintexWorkflowWS();
2. Ensure the web method will execute against the correct target URL:
objWFService.Url = "http://ltp-21:17819/MyNintexSite/_vti_bin/nintexworkflow/workflow.asmx";
This URL must be the full URL to the target SharePoint site followed by “/_vti_bin/nintexworkflow/workflow.asmx”.
Please ensure that you replace the first part of the URL with that of your target environment.
3. Set the credentials for the web service:
objWFService.Credentials = System.Net.CredentialCache.DefaultCredentials;
4. Read the Nintex workflow file (.nwf) into a byte array:
byte[] arrWorkflowFile = System.IO.File.ReadAllBytes(@"E:\WorkflowsToImport\UpdateTitlewithTime.nwf");
This is the workflow which we exported earlier to a local file.
5. Call the web method to import and publish the workflow file.
objWFService.PublishFromNWF(arrWorkflowFile, "MyDocs", "My Imported Workflow", true);
The parameters are:
1. arrWorkflowFile: Byte Array of the workflow file to be imported.
2. listName: The name of the target list / library.
3. workflowName: The name which will be assigned to the new imported workflow.
4. saveIfCannotPublish: Flag to indicate whether the workflow must be saved if it cannot be published.

Verify Success:
Run the code and step through each line to ensure there are no exceptions.
After successful execution open the target SharePoint site and the target document library / list.
Go to Manage Nintex Workflows and verify that the workflow was successfully imported and published – you should see the new published workflow.
image
Conclusion:
As you can see from the sample code it is really easy to automate the import and publishing of Nintex 2010 workflows to SharePoint 2010. You might come across a few exceptions if your environment does not allow certain workflow actions or if dependencies (like list or field lookups in workflows) are not in place in the target environment.
You can now use the very basic sample code to develop a tool like the one I illustrate at the top of this post to automate workflow upload & publishing and hopefully save your system administrator hours of manual configuration.
Enjoy !!

7 comments:

Paul said...

Interesting, but why didn't you use the Call Web service action within a Nintex workflow rather than coding it?

chromosoom said...

@Paul, You can use the code when you would like to package your Nintex workflow(s). On my blog: (http://alottolearn.net/2011/11/07/package-nintex-workflows/) I explain in a few steps how to use the code like this one in a SharePoint WSP.

Claire said...

Very useful post !
What if the Nintex Workflow uses Infopath forms (.xsn files). How to deploy thoses files too ? Thanks

Anonymous said...

Hi Could you publish the code?

Anshu said...

Hi Johan,
I am running into the same situation. I have to deploy a Nintex workflow with my wsp. I will try your solution. Can you please send me the entire code to this email?
abusaco@gmail.com

Anonymous said...

Hi,

In nintex where xsn file get stored?

thanks,
Shardul

bigpatty said...

Hi, I just found your post. Very interesting. I have tried two similar approaches,, one was an windows executable and the other a nintex workflow to publish other workflows. The executable was work but now is bust. I would really like to see your code for this. I have to publish an updated workflow to a single list in over 1500 subsites and it's driving me crazy as I am running into so many small issues.
Please,,, I would be so happy to see your solution and try to change it to run on all sites under a parent site.
Thanks,

Alan

Post a Comment