Skip to main content
Community site session details
Power Automate - Building Flows
Answered

Auto-assign File names in a SharePoint Site

Like (0) ShareShare
ReportReport
Posted on 22 Dec 2023 14:20:01 by 5

Hello all,

 

I am trying to set up a flow that will auto-assign a unique file name to a document when it is added to a SharePoint library. I want it to work across multiple libraries in the same SharePoint site. The file name I want is KRATOP-1000 and the number would change with each document. Any help or insights on building this flow is appreciated.

  • Verified answer
    creativeopinion Profile Picture
    10,458 Super User 2025 Season 2 on 22 Dec 2023 at 18:57:19
    Re: Auto-assign File names in a SharePoint Site

    @mastarke 

    Thanks for the info. This would be my recommendation. Tip: Rename your actions to keep your flow organized and run tests at each stage of the flow to make it easier to troubleshoot.

     

    Something to note: You'll need to decide on a trigger for your flow. Unfortunately, the triggers for when a file is created requires you to specify a document library. Unless you want to create a separate flow for each of your libraries (not recommended), you'll need to run your flow on a schedule.

     

    For now, I'd recommend using a Manual trigger while you build and test your flow. You can adjust the trigger later once you've confirmed the flow works. 

     

    Get Last File Name from SP List

    First you'll need to find out what the last file name was assigned so you can determine which file name to assign next. 

     

    Add a Get items action to your flow and select the list that has your file names. Since your file names are sequential, you can use the Order By field, to order your list by Title in descending order.

     

    Title desc

    creativeopinion_1-1703353115215.png

    I would also recommend limiting the Top Count to 1. 

     

    Compose Last File Name

    Add a Compose action to hold the last file name.

    creativeopinion_6-1703353341261.png

     

    Since the Get Items action returns an array of items (even if there is only one item in the array) you'll need an expression to avoid the Apply to Each action. For additional Tips and Tricks when using the Apply to Each action—check out this YT Tutorial: 3 Mistakes YOU 🫵 are Making with the Apply to Each Action in your Microsoft Power Automate Flow

     

    Insert a question mark, zero between square brackets and Title between square brackets and single quotes.

    creativeopinion_2-1703353150423.png

     

    Press the UP arrow key to go to the start of the expression and select the Dynamic content tab and insert the value dynamic content from the Get Items action.

    creativeopinion_3-1703353183029.png

     

    Run a test. Review the output of the compose action and ensure that it's what you are expecting.

    creativeopinion_4-1703353223216.png

     

    Filter Array – Document Libraries Only

    Next, you'll need to get all the Document Libraries on your site. The Scope action is optional. I like to use Scope actions in my flows to keep things organized. It also allows you to quickly collapse multiple actions at once.

     

    Add a Get all lists and libraries action to your flow. This action will return all lists and libraries. Refer to this section of my YT Tutorial: Are you using the Microsoft Power Automate Filter Action wrong?  where I cover how to filter out Document Libraries only with the Filter array action. 

    creativeopinion_5-1703353242517.png

     

    Add a Filter Array action and select Edit in Advanced mode.

    creativeopinion_7-1703353390624.png

    Insert this expression to filter out Document Libraries only. Tip: You can add additional conditions to your Filter Array action if you want to omit specific Document Libraries from this flow. By removing any document libraries you don't want the flow to check—you can speed up your flow run. Refer to this section of the YT tutorial where I cover how to add multiple conditions to the Filter Array action. 

    @equals(item()?['Type'], '101')

    Add a Compose action (optional) to return the count of libraries—use the length() function. This can be helpful with troubleshooting.

    creativeopinion_9-1703353488520.png

    Select the Dynamic content tab and insert the Body dynamic content from the Filter Array action into the length() function.

     

    creativeopinion_10-1703353515322.png

     

    creativeopinion_8-1703353413749.png

     

    Run a test. Confirm the number of document libraries returned is correct.

    Loop through Each Doc Library

    Add an Apply to Each action to loop through each Doc Library. Insert the Body dynamic content from the Filter Array action.

    creativeopinion_11-1703353565179.png

     

    Add two Compose actions (optional—but recommended) to store the Name and DisplayName dynamic content from the Filter Array action.

    creativeopinion_12-1703353580381.png

     

    Ensure you are selecting the dynamic content from the Filter Array action and NOT the Get lists and libraries action. 

    creativeopinion_13-1703353625903.png

     

    Run a test. You can should see that the Apply to Each action is looping through each Library returned from the Filter Array action. 

    creativeopinion_13-1703268698344.png

     

    Compose Timestamps

    There needs to be some way you can filter our specific files that need to be renamed. Otherwise your flow will take a very long time to run. In my particular case I'm going to only search for Files Created today. You will need to adjust this portion of the flow to suit your needs.

     

    Add a Convert time zone action. 

    Base Time: Insert the utcNow() function.

    Source Time Zone: Search for Coordinated Universal Time and select it.

    Destination Time Zone: Search for your local time zone and select it.

    Format string: Search for Round Trip and select it.

    creativeopinion_14-1703269199112.png

     

    This will return the current timestamp. You'll also need to compose a timestamp for 12AM today (again, adjust this to suit your needs). I'm assuming you'll be running this flow once a day—maybe at the end of the day. You could also run it multiple times in a day (eg. every hour or every couple hours etc).

     

    Add a Compose action. This is optional however, I prefer using Compose actions to store my dates and times so I can run a test to ensure the values are what I expect before using them elsewhere in my flow.

    creativeopinion_16-1703353734569.png

     

    Add an expression and insert the formatDateTime() function.

    creativeopinion_17-1703353749738.png

     

    Insert the Converted time dynamic content. 

    creativeopinion_18-1703353765778.png

     

    Add a comma and single quotes. In between the single quote insert this datetime format:

    yyyy-MM-ddT00:00:00

    creativeopinion_19-1703353795082.png

     

    Run a test. Confirm the outputs. The Convert Time Zone action will return the current timestamp and the Compose action will return the timestamp for today at 12AM.

    creativeopinion_21-1703353847384.png

     

    Get Files for Each Document Library Created Today

    Add a Get files (properties only) action. In the Library name field, insert the output from the Compose action that has the Library Name—not the Library Display Name.

    creativeopinion_22-1703353884581.png

    Insert a Filter Query. The Filter query will check for files created greater or equal to 12AM today, and less than or equal to the current timestamp.

    creativeopinion_23-1703353930555.png

    Whenever I use a Filter Query in a Get files (properties only) action, I always like to return the count of items returned in a Compose action. This is helpful when building a flow and can also be used to troubleshoot your flow. 

    creativeopinion_24-1703353943671.png

     

    Run a test. Review the outputs. 

    I'll add a few Scope actions to group some of these actions together so I save on vertical space in the flow.

    creativeopinion_26-1703354002454.png

    Filter Files

    You'll need to filter your files additionally to filter out any files that have already been renamed. This will be a must in your flow especially if you plan to run it multiple times a day. Unfortunately the Filter Query doesn't allow you to filter out items by file name—you'll need to use a Filter Array action instead.

     

    For example I have two files created today. One of them has been renamed with the last file name in the SP List.

    creativeopinion_27-1703270671166.png

     

    Because your filename has a prefix, you can use this to filter out files that start with your prefix.

     

    Add a Filter Array action (I've nested mine inside the a Scope action). Insert the value dynamic content from the Get files (properties only) action. 

    creativeopinion_30-1703354267887.png

     

    I'm not exactly sure why the dynamic content from the Get files (properties only) action isn't' visible to me currently. Might be a bug. If this is the case for you, you can access the dynamic content using the dynamic content key and the item function.

    In the first value field, insert the item() function, question mark, square brackets and single quotes:

    item()?['']

    creativeopinion_31-1703354341592.png

     

    To return the file name with extension, the key is:

    {FilenameWithExtension}

    creativeopinion_32-1703354353864.png

     

    Change the operator to does not start with. In the second value field insert your file prefix.

    creativeopinion_33-1703354391194.png

     

    Add a Compose action to store the count of Filtered items.

    creativeopinion_34-1703354407548.png

     

    Run a test. Confirm the outputs of the Compose action storing the number of Files to Rename (filtered files). Below you can see 2 files were returned from the Get files (properties only) action. However, since one of those files starts with the prefix, the Filter Array action filtered that item out.

    creativeopinion_35-1703354480407.png

     

    Condition Check

    By checking to see if files that meet your criteria exist, you can build a more efficient flow. Add a Condition check to see if the Compose action is not equal to 0. This means that there are files. If there are files, insert the actions you want the flow to take in the Yes branch. If there aren't any files, you can leave the NO branch empty—or add additional actions to that branch.

    creativeopinion_36-1703354509525.png

     

    Loop through Each File

    Next add an Apply to Each action to loop through each file returned.

    creativeopinion_37-1703354530366.png

     

    This next step is optional, however this can help confirm the name of the files returned from the Get files (properties only) action. Especially when you have a flow with many actions, sometimes the incorrect dynamic content is selected which throws your whole flow off. 

     

    To return the file name of the current item being looped through, you'll need to use the same expression as you used above in the Filter Array action.

    item()?['{FilenameWithExtension}']

    Add another Compose action to store the ID of the file (which you'll be using later). The key for the ID is ID.

    item()?['ID']

    creativeopinion_38-1703354591569.png

    Run a test. Confirm the names and IDs of the files returned (aka the Files you will be renaming)

    creativeopinion_39-1703354670098.png

     

    Get File Extension

    I'm unsure if the file extension for the files you will be renaming will always be the same, if not, it might be a good idea to add a Compose action to hold the file extension. You can do this by using an expression an the 

    use a split() function. The split() function takes two parameters. For a demo on how the split() function works—refer to this section of a YT Tutorial I recently uploaded.

     

    The split() function takes 2 parameters:

    split([string],[separator])

    Insert the output from the compose action that is storing your file name and extension.

    creativeopinion_50-1703273822291.png

    Add a comma and single quotes. Between the single quotes enter your separator. In this case it's a period.

    creativeopinion_40-1703355490930.png

     

    This expression will split the file name at the period—essentially returning an array of values. The text before the space and opening bracket will be the first value, the text after the space and opening bracket the last value.

    creativeopinion_41-1703355527095.png

     

    To return the file extension only, you'll need to wrap the entire expression in the last() function. Place your cursor at the start of the expression by pressing the up arrow key. Type in last with an opening bracket.

    creativeopinion_52-1703274002362.png

    Next, place your cursor at the end of the expression by pressing the down arrow key and insert a closing bracket. This expression will return text after the hyphen (aka the number).

    Run a test. Confirm the expression is returning the file extension only.

    creativeopinion_54-1703274084235.png

    Compose the New File Name

    We'll need to Compose the new file name based off of the last file name in your SP list. First, you'll need to initialize an integer variable in the root of your flow (it can't be nested within any actions). I've added the action just before the Apply to Each action.

    creativeopinion_40-1703272886556.png

     

    You'll need to set the initial integer. We'll grab this from the Last File Name Compose action. To return the number only from the Last File Name, you'll need to use an expression.

    creativeopinion_56-1703274172436.png

    Use the split() function and insert the output from the Compose action with the last file name—as you've done above in the prev step.

    creativeopinion_42-1703273024752.png

    Add a Comma and single quotes. Between the single quotes enter your separator. In this case it's a hyphen.

    creativeopinion_43-1703273296535.png

     

    This expression will split the text string at the hyphen—essentially returning an array of values. The text before the space and opening bracket will be the first value, the text after the space and opening bracket the last value.

     

    To return the number only, you'll need to wrap the entire expression in the last() function. Place your cursor at the start of the expression by pressing the up arrow key. Type in last with an opening bracket.

    creativeopinion_44-1703273434877.png

     

    Next, place your cursor at the end of the expression by pressing the down arrow key and insert a closing bracket. This expression will return text after the hyphen (aka the number).

    creativeopinion_42-1703355591939.png

     

    Lastly, you'll need to convert the string of text into a number by wrapping the entire expression in the int() function. 

     

    Place your cursor at the start of the expression by pressing the up arrow key. Type in int with an opening bracket. Next, place your cursor at the end of the expression by pressing the down arrow key and insert a closing bracket.

    creativeopinion_46-1703273484733.png

     

    creativeopinion_43-1703355604399.png

     

     

    Run a test. The Initialize Variable action should contain the number only.

    creativeopinion_48-1703273567984.png

    Inside the Apply to Each loop that is looping through each file. Insert an Increment variable action. This will increment the variable you set in the root of the flow by 1 (each time the apply to each action loops through a file.

    creativeopinion_44-1703355644471.png

     

    We'll compose the new file name and test it out before moving on with building the rest of the flow. Add a Compose action. 

     

    Enter the file prefix and a hyphen. Insert the variable and a period. Insert the output from the Compose action with the file extension.

    creativeopinion_57-1703274251055.png

     

    Run a test. Check the output of the Compose action. Ensure it's what you are expecting.

    creativeopinion_58-1703274339687.png

    Rename File

    Add a Send an HTTP Request action. You'll be adding two of these actions. First one will be to get the List Item Entity Type.

     

    Select your site and leave the Method as GET. In the url field enter:

    _api/lists/getbytitle('[Library DisplayName]')

    Replace the red text with the output from the Compose action that has the library Display name. 

    creativeopinion_37-1703272489362.png

    Run a test. View the outputs of the action. We need to return the ListItemEntityTypeFullName. If you scroll to the bottom you'll find the dynamic content. 

    creativeopinion_63-1703274706531.png

     

    Add another Send an HTTP request. You'll use this action to rename the files. Select your site address and change the Method to POST.

     

    In the URL field, enter:

    _api/lists/GetByTitle('@{outputs('[Library DisplayName]')}')/Items([File ID])

    Replace the red text with the appropriate dynamic content.

    creativeopinion_59-1703274482931.png

    For the headers click on the icon to switch to text mode. Paste in this JSON:

    {
    "content-type": "application/json;odata=verbose",
    "IF-MATCH": "*",
    "X-HTTP-Method": "PATCH"
    }

    You can press that icon again to switch the mode.

    creativeopinion_61-1703274524418.png

    In the Body field, paste in this text:

    {'__metadata':
    {'type':'[ListItemEntityTypeFullName]'},
    'FileLeafRef':'[New File Name]'}

    Replace the ListItemEntityTypeFullName with an expression. Because it's nested within the 'd' dynamic content key, you'll need to start your expression this way:

    ?['d']?['ListItemEntityTypeFullName']

    creativeopinion_71-1703275225054.png

    Click on the dynamic content tab and insert the body output from the previous Send an HTTP request action.

     

    Replace the New File Name with the output of the Compose action that has the new file name.

     

    Run a test.

    creativeopinion_72-1703275783020.png

     

    If you'd like to include a hyperlink to the file in your SharePoint list, you can take some inspiration from my YT Tutorial: Create Folders and Hyperlinks in Bulk with this Microsoft Power Automate Flow

     

    Although this tutorial covers how to create hyperlinks to folders—you can take the same concept and adjust it so it links to a file instead. The tutorial will cover:

    âś… Add a Hyperlink with Custom Display Text to a SharePoint list

    âś… Use the Send HTTP Request to SharePoint action

     

    You can include the actions in this flow so that your SharePoint list will also have a link to your renamed files!

     

     

    Hope this helps!


    If I helped you solve your problem—please mark my post as a solution ✅.
    Consider giving me a 👍 if you liked my response!

    👉Watch my tutorials on YouTube
    👉Tips and Tricks on TikTok

     

Helpful resources

Quick Links

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 497 Super User 2025 Season 2

#2
David_MA Profile Picture

David_MA 436 Super User 2025 Season 2

#3
Riyaz_riz11 Profile Picture

Riyaz_riz11 244 Super User 2025 Season 2