Recently we had a customer ask us to automate the creation of structured folders with Power Automate for new projects and to also generate and assign a project number. When a user creates a new folder for the project, the folder should automatically be renamed to "UKXXXX - Folder Name". For example, if the user creates a new folder named "Important Project" then it should be renamed to "UK1234 - Important Project". Then a set of subfolders should be created within it, as shown below.
When first looking at the requirements it seems relatively simple, but I soon realised that there was a complication that I had not considered. There is no action in Power Automate to rename a folder.
Before creating the Flow, I created a document library to store the project documents and a list to store the next project number.
I called the document library "Live Projects". No metadata columns are required but I can add these in the future if needed to assist with searching.
I called the List "Next LP Number", it is very simple and only contains one item. The item has an ID of 1. It is probably a good idea to apply some permissions on the list to ensure that nobody can delete the item. The list has a number column to store the next project number. Just add the first item with the number of the first project. As below.
The first step of the Flow is to add the trigger. In this case I used "When a file is created (properties only)". This is because I want the Flow to be triggered when a new folder is created.
Then I created some variable that I will use in the Flow.
'Current Name' is initialised with the path of the folder that has triggered the Flow. This is relative to the site address.
Next the Flow needs to check that the item that was created is a folder and that the folder is at the root of the document library. This bit is important because without the check, each folder in the set that is created later will cause the Flow to trigger again and again and again... I discovered this to my cost, and I will not make the same mistake again.
Checking that the item is a folder is simple. Checking that the new folder has been created at the root of the document library is more complex.
I used indexOf(variable('Current Name'), '/') is equal to lastIndexOf(variables('Current Name'), '/') to check that there is only the one '/' in the string variable 'Current Name. This works well for me, but I am sure there are other methods.
If the result of the condition is False, then the Flow does nothing and exits. If the result is True, then the Flow continues to the next step.
First get the next project number from the list we created earlier.
Now the variable 'job' has been set to the project number, so we have enough information to create the new name for the folder.
The variable 'New Name' is now set correctly, so the project number can be incremented ready to be saved back to the list for next time.
We can now rename the new folder, but there is no action available to do this, so we need to use "Send an HTTP request" as shown here.
Now we can save the next project number back to the list. This is done by updating list item 1 with the variable 'Job'.
All that remains now is to create the set of folders using the "Create new folder" action for each of the required folders. Repeat the action as many times as needed.
That is the end of the Flow, but as usual there is more that can be added. For example, a notification could be sent out to project managers and a set of tasks created to manage the project.
Power Automate is an incredibly powerful application that offers a variety of ready made templates to help you get started.
Read our recent post on the retirement of SharePoint 2010 Workflows here.