How would you go about automating copying all folders and their files from one Document Library to another using Nintex Workflow?
SharePoint exposes 2 web services that help us here. The Lists and Copy web services.
The Lists web service lets us query the source document library using the GetListItems web method and find out what files we have that need to be copied.
The Copy web service allows us to copy files from one location to another using the CopyIntoItemsLocal web method.
First, we use the Nintex Workflow Query List action to query the source document library, to get all the folders. In this article, my document library only has folders in the root of the document library so we can be sure that by running a Query List action, we will only get a list of folders.
We store this in a Collection variable and then iterate through the collection using a For Each action.
n the For Each action, we call the Lists web service to get the list of files in the current folder. The result is in XML, so we use the Query XML action to parse the XML using XPath to get the filenames.
We should now have a collection variable that contains the filenames of all the files in the folder we are currently looking at. Now we need to iterate through the collection of filenames and copy them to our destination folder.
In order to use the CopyIntoItemsLocal web method, we need to build up the source and destination URLs. To do this, we create 2 Nintex Workflow text variables and use the Build Dynamic String action to create the 2 URLs.
The final 2 actions of the workflow are the core of this workflow. We need to create the destination folder and then copy the source item into that folder.
We can use the Create an Item action to create the folder, because if this folder does not exist, it will create it, and if it does exist, then it will not throw an error and break the workflow.
Now that we are sure the folder exists, we can use the CopyIntoItemsLocal web method in the Copy web service to copy the item.
This workflow will run on an item in the destination Document Library, and copy the folders and their contents from a source library. I hardcoded the source library name, but there really isn’t any reason why it couldn’t be used dynamically.
As I’ve mentioned in previous articles, this is not necessarily a solution that you can import into your environment and just use. It is however, a good starting point if you have a need to do something of this nature.