In this blog post I will show how to dynamically switch SharePoint Document Libraries based on a string to perform further actions like creating a folder. I’ve used this pattern for dynamic folder creation where the sheer scale of folders required dedicated Document Libraries based on alphabetic ranges (A-F, G-M, N-T, U-Z).
My demo SharePoint site has four additional document libraries called: “A-F”, “G-M”, “N-T” and “U-Z”. I’ll be creating folders based a persons last name and to a void duplication I will create the folder as: Last name, First name (Claims name)
.

My demo list has two columns: “Name” (Person column type) and “FolderLocation” (URL).

Create a new Flow by clicking “Automate” > “Power Automate” > “See your flows”.

Click “New” > “Automated-from blank”.

Provide a Flow name, I.e. “Demo Flow”, search for SharePoint and select the “When a item is created” trigger and click “Create”.

Set the “Site Address” and “List Name” to you target site and list.

Click “New step”, search for “Variable” and select the “Initialize variable” action.
Set “Name” to “varFolderName”, “Type” to “String” and “Value” to the following expression:
concat(last(split(triggerOutputs()?['body/Name/DisplayName'], ' ')), ', ', first(split(triggerOutputs()?['body/Name/DisplayName'], ' ')), ' (', first(split(last(split(triggerOutputs()?['body/Name/Claims'], 'i:0#.f|membership|')), '@')),')')

I’ve written about the SPLIT, FIRST and LAST functions before and it looks more complicated than it is. See Tech Community: Naming attachments for more info.
Create another string variable called “varDocumentLibrary”.

Click “New step”, and select the “Condition” action. Set:
- “Choose a value” to “varFolderName”
- Condition to “is less than or equal to“
- “Choose a value” to “F”

In the “If yes” branch add a “Set variable” action and set the “Name” to “varDocumentLibrary” and “Value” to “A-F” where “A-F” is the document library name.

My completed “Condition” action looks like the image below.

Create three additional “Condition” actions based on the following criteria:
- Condition (G-M)
- “varFolder”, “is greater than or equal to”, “G”
- “varFolder”, “is less than or equal to”, “M”
- Set variable (G-M)
- “Name”, “varDocumentLibrary”
- “Value”, “G-M”

- Condition (N-T)
- “varFolder”, “is greater than or equal to”, “N”
- “varFolder”, “is less than or equal to”, “T”
- Set variable (N-T)
- “Name”, “varDocumentLibrary”
- “Value”, “N-T”

- Condition (U-Z)
- “varFolder”, “is greater than or equal to”, “U”
- Set variable (U-Z)
- “Name”, “varDocumentLibrary”
- “Value”, “U-Z”

Click “New step”, search for “SharePoint” and select the “Create new folder” action.
Set the “Site Address” to you target site, “List or Library” to “varDocumentLibrary” and “Folder Path” to “varFolderName”.

Click “New step”, search for “SharePoint” and select the “Send an HTTP request to SharePoint” action.
Set the “Site Address” and “List Name” to you target site and list.

Also set:
- “Method” to “POST”
- “Uri” to “_api/web/lists/GetByTitle(‘Demo‘)/items(ID)
- Where ‘Demo’ is the list name and ID is the “ID” from the SharePoint “Create new folder” action
- “Headers” – switch to text – to:
{
“Content-Type”: “application/json;odata=verbose”,
“X-HTTP-Method”: “MERGE”,
“IF-MATCH”: “*”
} - “Body” to:
{‘_metadata’: {‘type’:’SP.Data.DemoListItem’},’FolderLocation‘: {‘Description’: ‘varFolderName‘, ‘Url’: ‘LinkToItem‘}
}- Where Demo is the list name; FolderLocation is the column name; varFolderName is the variable and LinkToItem comes from the “Create new folder” action.
My completed Flow looks like the image below:

Save your Flow and test by submitting a new item to your list. If all goes well the new folder will be created in the document library based on the folder name and a link to the folder will be added to the list item.

I like this pattern for scenarios where folder naming conventions and storage locations are important to the business process, especially at scale. The user friendly link stored in the list means the user is one click away from the folder contents.
Thanks for reading.
NY
Norm, great post as always. I’ve done something similar in past with variables for document libraries but have used a List or JSON document to store master libraries etc which I can reuse. I’ve also started looking at using Site Designs within Power Automate again to reuse artifacts.
LikeLike
Sounds awesome. Are you blogging, vlogging this stuff?
LikeLike