Rename Folder using Microsoft Flow / Power Automate in a Document Library in SharePoint Online

Hi Friends,

Today blog is about renaming the folder in the document library using the MS FLOW or MS Power Automate. When I started working on this it seems to be an easy job, but if you look into the connectors you'll find it really difficult as there is no straight forwards connector to do the job.




Issues encountered

  • If you try using the "Get Folder Metadata" connector, you'll not be able to get the ID of the folder as it is returning as the negative value.
  • If you get the ID somehow and try to update the folder name using the "Update File Properties" connector, you'll end up renaming only the title of the folder, but it will still have the old name as the display name which in case is again a failure.
  • There is no other connector which can be utilized to achieve the task of renaming the folder in a document library directly.

Solution

The solution is a simple three-step (at least in this example).

Step 1: Initialize variables

In case you are computing values for the old name and new name, get them into the variables. In this case, I have added them to the variables

Rename Folder using Microsoft Flow / Power Automate in a Document Library in SharePoint Online

Step 2: Use of "Send an HTTP Request to SharePoint" connector

This is the only way to rename the folder. Add the following configuration:

Site Address: <Address to your site>
Method: "POST"
Uri: "_api/web/GetFolderByServerRelativeUrl('Shared Documents/FolderName')/ListItemAllFields"
Headers
  • If-Match: *
  • X-HTTP-Method: MERGE
  • Accept: Application/json;odata-verbose
Body: {
              "Title": "NEW FOLDER NAME",
              "FileLeafRef": "NEW FOLDER NAME"
            }

Rename Folder using Microsoft Flow / Power Automate in a Document Library in SharePoint Online


Step 3: Test Your Flow

Now test your flow, and you'll see that the folder name is now renamed to the new name.

Rename Folder using Microsoft Flow / Power Automate in a Document Library in SharePoint Online


Hope this article would have helped you

Happy Coding..!!
#PowerAutomate #Microsoft #SharePointWidgets

Comments

  1. Thank you. Quite helpful. You saved us a great deal of time! most appreciated. Thanks again

    ReplyDelete
  2. Hello, could you please show how to initialise variables? For Old Name I'm using "NAME" of the initial folder but it does not work for me.

    ReplyDelete
    Replies
    1. Old Name means the current name of the folder. and New Name means the name of the folder you want the folder to update to.

      Delete
  3. Thank you, this work, but I had to modify the header Accept:"application/json;odata=nometadata"

    ReplyDelete
  4. Thanks. But my file is in a document library so should I change the url to the library if so how? Site address only goes up to the site. so I added document library name to URi (_api/web/GetFolderByServerRelativeUrl('PENM/@{variables('OldFile')}')/ListItemAllFields) replacing Shared Documents. But then the flow stops with an error message "Invalid Request"

    ReplyDelete
    Replies
    1. Hi, I think you misunderstood, the flow does not changes the name of the library (you are referring to as folder in this case Shared Documents), but this article helps you to udate the folder name within these librsries.

      Delete
  5. hello,

    Inside my document library i have a template folder which i would like to be copied and renamed. will this work?

    ReplyDelete
  6. I want to thank you for posting this solution. I looked all over and came across this and it worked the very first time. I don't understand fully the HTTP request, but I did enough to know how to apply your solution. THANK YOU VERY MUCH!!!!!

    For my purposes I also created a step prior to the HTTP request in order to explicitly get the folder ID from another metadata column (similar to a LookUp), then Set the OldFolderName variable based on that "LookUp" result .

    ReplyDelete
  7. Thank you. Looks good. I'm looking at renaming subfolders based on the parent folder name, so for example, parent folder = xxxx. Subfolders are aaa, bbb, etc. Result is subfolder renamed to xxxxaaa, xxxxbbb etc. Can the flow be amended to that?

    ReplyDelete
  8. This is a great solution! Thank you for sharing! How can I change item's content type using similar approach (we can't use Update Item because of lookups limit error)?

    ReplyDelete
  9. Hi there, I would just like to ask if I would like to take the current folder with the name (e.g. Jan'23) and would like to set the automation for it to change it to the following month and year (e.g. Feb'23). Is that possible by using or altering your current code?

    ReplyDelete
  10. I tried to follow this but nothing happens in my folder name but the flow is successful

    ReplyDelete
    Replies
    1. Check your search pattern to the folder you want to change. I had wrong search path and didn't get any error.

      Delete
  11. I have the same problem as "AnonymousFebruary 20, 2023 at 2:25 PM"
    My flow runs without error, but the folder name does not change.
    I have a flow that is activated from a MS Form "Name your Folder".
    It first copies the folder from a template library.
    The next step is to rename the folder to the name given in the MS Form.
    I will try to put in a timer and experiment with that, but if you have a solution it would be great.

    ReplyDelete
  12. Thanks, just what I needed

    ReplyDelete
  13. Hi, could you help me? I already tried any possible solution (that i found) but didn't work.
    I have a library called 'Forms Documents', and into have the folders 'Location 1', 'Location 2',... and subfolders 'Equipment 1', 'Equipment 2'... Every time that I run the flow, the action returns status code 400 and I dont't get the folder data.
    I think the error is on URI field, I'm putting this information here: _api/web/GetFolderByServerRelativeUrl('/Form Documents/Location 1/Equipment 1')/ListItemAllFields.
    I've tried a lot of others sintaxe, but didn't works...
    Do know what the error could be?

    ReplyDelete
  14. There are times that Send HTTP Request is failing due to "BadGateway" but I couldn't figure it out why

    ReplyDelete

Post a Comment

Popular posts from this blog

Power Automate: How to Add "New Line" to the text in SharePoint multiline text field

Power Automate: Rename file in SharePoint Online