Power Automate: SharePoint Online | Folders to Document Sets


Nested folders are abundant in many, many SharePoint Online (SPO) tenants. Largely because a lot of organizations aren’t ready to adopt a flat content architecture. Fortunately, plenty of their root directories adhere to strict folder naming conventions, so several of those root folders can be recreated as document sets. One perk of document sets is reducing overall folder path lengths and avoiding potential problems in the near future.

E.g., extract client city, state, and postal code from long folder names.

Figure 1 - SharePoint Online document library with root folders with long names.
Figure 1SharePoint Online document library with root folders with long names.

Document sets are essentially a special type of SharePoint folder with metadata applied. The client information from these regular folders can be extracted and used to populate the document set values, creating a new folder with a new icon and a reduced path length:

E.g., assign client city, state, and postal code as metadata values.

Figure 2SharePoint Online document library with document sets with metadata values.

For ease, these metadata columns were created as Single line of text fields, but additional columns could be created with different data types for even better content organization:

Figure 3SharePoint Online document set settings with column types.

To automate the provisioning process, create an Instant cloud flow with Power Automate and initialize some variables for storing the extracted metadata values. In this example, there is the client’s name, city, state abbreviation, and postal code.

Also, for consideration, create temporary variables for flow data objects and strings:

Figure 4 - Power Automate cloud flow with initialize variable actions.
Figure 4Power Automate cloud flow with initialize variable actions.

FYI, object values are stored and retrieved using key-value pairs:

variables('tempObj')['Name']
Figure 5 - Power Automate cloud flow variable with key-value pairs.
Figure 5Power Automate cloud flow variable with key-value pairs.

Next, get a list of root folders from the source library. Use the List folder action and return an array of folders. Have the flow loop through this array and for each record, set the folder data to the temporary object variable, then set the folder name to the temporary string variable:

Figure 6 - Power Automate cloud flow actions to query library folders, then iterate through the array.
Figure 6Power Automate cloud flow actions to query library folders, then iterate through the array.

One thing to be cautious of, when using the List folder action to get library root folders, the array includes the hidden “Forms” folder. Iterating and reading this folder isn’t a problem. However, this folder should be ignored because accidentally trying to recreate it as a document set will cause the cloud flow to hang:

Figure 7 - Power Automate cloud flow condition to ignore the "Forms" folder.
Figure 7Power Automate cloud flow condition to ignore the “Forms” folder.

Inside of the TRUE condition block, for each array record, extract the client’s name, city, state abbreviation, and postal code from the temporary string variable:

E.g., extracting the client’s name.

trim(
   last(
      split(variables('tempStr'), '-')
   )
)

Perform similar extractions and set the other values to their respective flow variables, then create the document set in the target document library.

Another interesting quirk, the cloud flow hung when trying to Create new document set if the item already existed. To avoid this, add a Get folder metadata by path action before the create action and only create if this action fails:

Figure 8 - Power Automate cloud flow actions to set variables and create document set if it doesn't already exist.
Figure 8Power Automate cloud flow actions to set variables and create document set if it doesn’t already exist.

Honestly, a flow variable for each value isn’t necessary, but it does make things easier when creating the document set. Because the values have already been set to their respective variables, just plug in each variable for the corresponding metadata column in the Create new document set action:

E.g., variable clientFolderName is the name of the new document set.

Figure 9 - Power Automate cloud flow action to Create new document set using flow variables.
Figure 9Power Automate cloud flow action to Create new document set using flow variables.

FYI, for the document set check action, use the Get folder metadata using path action because again, document sets are still folders, just more fancy folders:

Figure 10 - Power Automate cloud flow action to Get folder metadata using path.
Figure 10Power Automate cloud flow action to Get folder metadata using path.

“You have the right to be involved. You have something important to contribute, and you have to take the risk to contribute it.”

Dr. Mae C. Jemison

Leave a comment