Use Power Automate to dynamically switch Document Libraries based on a string

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).

Document Libraries A-F, G-M, N-T and U-Z.

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”.

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”.

Demo Flow, SharePoint, When an item is created

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

When an item is created action

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|')), '@')),')')

Initialize variable varFolderName

I’ve written about the SPLITFIRST 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”.

Initialize variable 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”
Condition for A-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.

Set variable

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

Completed condition

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 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 N-T
  • Condition (U-Z)
    • “varFolder”, “is greater than or equal to”, “U”
  • Set variable (U-Z)
    • “Name”, “varDocumentLibrary”
    • “Value”, “U-Z”
Condition 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”.

Create new folder

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. 

Send an HTTP request

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:

Completed Flow

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

2 thoughts on “Use Power Automate to dynamically switch Document Libraries based on a string

  1. 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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s