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 !!

14 comments:

Paul said...

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

Anonymous 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

Diomos said...

Ho can I publish site wf?

Anonymous said...

Hi trying to mimic you code for office 365 any advise ? also can you email me the code please for the article above. ruanswanepoel18@yahoo.com

Anonymous said...

Tools for migration of Nintex office 365 in MetaVis metavistech.com very soon

Anonymous said...

I am in the process of deploying the NWs to multiple sites. I would appreciate if you could send me the full code to rastha2@outlook.com. Thank you.

How to stop smoking weed said...

Tech Gadgets reviews and latest Tech and Gadgets news updates, trends, explore the facts, research, and analysis covering the digital world.
You will see Some Tech reviews below,

lg bluetooth headset : You will also wish to keep design and assorted features in mind. The most essential part of the design here is the buttonsof lg bluetooth headset .

Fastest Car in the World : is a lot more than the usual number. Nevertheless, non-enthusiasts and fans alike can’t resist the impulse to brag or estimate according to specifications. Fastest Car in the World click here to know more.

samsung galaxy gear : Samsung will undoubtedly put a great deal of time and even more cash into courting developers It is looking for partners and will allow developers to try out
different sensors and software. It is preparing two variants as they launched last year. samsung galaxy gear is very use full click to know more.

samsung fridge : Samsung plans to supply family-oriented applications like health care programs and digital picture frames along with games It should stick with what they know and they
do not know how to produce a quality refrigerator that is worth what we paid. samsung fridge is very usefull and nice product. clickcamera best for travel: Nikon D850: Camera It may be costly, but if you’re trying to find the very best camera you can purchase at this time, then Nikon’s gorgeous DX50 DSLR will
probably mark each box. The packaging is in a vibrant 45.4-megapixel full-frame detector, the picture quality is simply wonderful. However, this is just half the story. Because of a complex 153-point AF system along with a brst rate of 9 frames per minute. camera best specification. click here to know more.

visit https://techgadgets.expert/ this site to know more.

Online e-Profile said...

Professional Digital Business Cards for all industries in Rs.99/-

Miniwebsite
Digital Visiting Card
Digital Marketing

Plumbing & HVAC Services San Diego said...

Plumbing & HVAC Services San Diego
Air Star Heating guarantees reliability and quality for all equipment and services.
Air Star Heating is specializing in providing top-quality heating, ventilating, air conditioning, and plumbing services to our customers and clients.
Our company is leading the market right now. By using our seamless and huge array of services. Our customers can now have the privilege of taking benefit from our services very easily and swiftly. To cope up with the desires and needs of our clients we have built an excellent reputation. We are already having a huge list of satisfied customers that seem to be very pleased with our services.

Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
Visit:- https://airstarheating.com

Post a Comment