Rename SharePoint folders in Power Automate

Have you ever wanted to rename SharePoint folders with Power Automate?

REST API call to use

According to the documentation the following two calls are needed to rename SharePoint folders.

The first call is to collect the odata type and the etag for the folder. The OData Type tells our REST API calls something about the shape of our data in our list. The etag identifies the document or in our case our folder that we want to update.

GET https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/ListItemAllFields
 Authorization: "Bearer " + accessToken
 Accept: "application/json;odata=verbose"

The second REST API call is used to do the actual update and to rename our SharePoint folder.

POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/ListItemAllFields 

Authorization: "Bearer " + accessToken 
Accept: "application/json;odata=verbose" Content-Type: "application/json" 
Content-Length: {length of request body as integer} 
If-Match: "{etag or *}" 
X-HTTP-Method: "MERGE" 
X-RequestDigest: "{form_digest_value}" 

{   
  "__metadata": {
     "type": "{odata.type from previous call}"   
                },
   "Title": "New name",
   "FileLeafRef": "New name"
}

Ok, that looks complicated!

Folder in SharePoint

In my document library in SharePoint, I’ve got a folder called testfolder.

This is the folder that I want to rename to a new name.

Rename SharePoint folders using Power Automate in 2 easy steps Microsoft Office 365 folder in a document library in sharepoint online

Send an HTTP request to SharePoint to get the OData.type and ETag

Time to have a look at how we call these REST API calls in Power Automate.

We will start with a Send an HTTP request to SharePoint action and then I’m adding two compose actions. These compose action aren’t really needed. But when we look at the Flow run they make it just that little bit easier to understand our data.

Send an HTTP request to SharePoint

In the above Send an HTTP request to SharePoint action Uri is set to include the SERVER relative url.

_api/web/GetFolderByServerRelativeUrl('/Shared Documents/testfolder')/ListItemAllFields

So if you happen to rename a folder in a site collection that isn’t your root site collection then you will need to include that part of the url too as shown below.

/sites/anysite/_api/web/GetFolderByServerRelativeUrl('/sites/anysite/Shared Documents/testfolder')/ListItemAllFields

The above compose actions collect the Type and the Etag with the following two expressions. For the Type this will be the following 1 liner

outputs('Send_an_HTTP_request_to_SharePoint_2')?['body/d/__metadata/type']

and for etag the following expression will do the job

outputs('Send_an_HTTP_request_to_SharePoint_2')?['body/d/__metadata/etag']

Now we can look at renaming our SharePoint folder with yet another Send an HTTP request to SharePoint.

Rename SharePoint folder

Now the final step of our flow to rename SharePoint folders using Power Automate. A Send an HTTP request to SharePoint. For more information about this action please see my user guide about using the SharePoint REST API

Rename SharePoint folder with Power Automate

The Uri in the above action is set to

_api/web/GetFolderByServerRelativeUrl('/Shared Documents/testfolder')/ListItemAllFields

The body of the REST API to rename SharePoint folders is set to

{
   "__metadata": {     "type": "@{outputs('_Compose-_Type')}"
   },
 "Title": "New Name",
 "FileLeafRef": "newname"
 }

In this example I’m going to set the Title and the FileLeafRef to a different value so that later on it is clearer to see what goes where. Quite often you might want to make these match.

Running the Power Automate flow

The first step in the flow is now collecting the etag and the type. Notice that the type will be different for every document library. But for every document library it will be the same. So you could even skip the first few steps and hard code the type if you really wanted to.

Rename SharePoint folders using Power Automate in 2 easy steps Microsoft Office 365 get the etag and type run output

Then the last step is now setting the Title and the name of the folder to the values that we want. the internal name of the Name field is FileLeafRef and that will be what we will need to use to compete our Power Automate flow.

Rename SharePoint folders using Power Automate in 2 easy steps Microsoft Office 365 setting the title and the fileleafref

As the flow is successful, we would hope that the folders have been renamed. Time to have a look back in SharePoint.

The Result

Now my folder in SharePoint has been renamed to newname.

Rename SharePoint folders using Power Automate in 2 easy steps Microsoft Office 365 new folder name has been applied

And the title has been set to New Name

Rename SharePoint folders using Power Automate in 2 easy steps Microsoft Office 365 title and name of the folder updated

And our mission to rename SharePoint folders has succeeded.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

27 thoughts on “Rename SharePoint folders using Power Automate in 2 easy steps”
  1. Thanks for this! I’m getting the following error when I’m trying this solution:

    “Correct to include a valid reference to ‘_Compose-_Type’ for the input parameter(s) of action ‘Send_an_HTTP_request_to_SharePoint’.”

    Any ideas?

    1. In the http request action you are referring to a compose action’s output. The name of the compose action is not right in the reference. What is the compose action called? Take the name and replace each space with an _

      1. Hey Pieter. Thanks for the reply! I found the error from your suggestion, but I’m getting another one on the first HTTP request. This flow is triggered when a new folder is created in SP, so that full folder path is captured in the trigger. I’m using the Full Path dynamic content field in the first get request like so:

        _api/web/GetFolderByServerRelativeUrl(@{triggerOutputs()?[‘body/{FullPath}’]})/ListItemAllFields

        But now I’m getting an error saying that expression is not valid. Any ideas?

      2. I didn’t add that line manually, it chose the Full Path field from the dynamic content dialogue box, it just pasted that way as text. I just tried removing the {} and I got another error that says:

        The expression “web/GetFolderByServerRelativeUrl(‘/@triggerOutputs()” is not valid.

      3. I just tried entering it manually to test it in the get & post request and got the same error from the get request:

        The expression “web/GetFolderByServerRelativeUrl(‘/Shared Documents/Projects/Marcela Test Folder/DJ QB SP TEST 18” is not valid.

        /Shared Documents/Projects/Marcela Test Folder/DJ QB SP TEST 18 is the same as what the dynamic content Full Path field would give me.

      4. The error message is pointing at the missing bit of the expression missing.

        ‘)

        Also the server relative path needs to include /sites/sitename if you are not using the root site collection.

      5. Hey Pieter,

        the ‘) are present in edit mode. Not sure why it’s not included in the error message. Also, I’ve got the site address listed in the first field of the HTTP request, just like you do. Do I need to restate part of the path in the Uri?

        Here’s what the failed step had as the source in the body of its outputs:

        “source”: “https://[site.url]/sites/[sitename]/_api/web/GetFolderByServerRelativeUrl(‘/Shared%20Documents/Projects/Marcela%20Test%20Folder/DJ%20QB%20SP%20TEST%2018#5709’)/ListItemAllFields”,

        Does anything seem wrong about that?

      6. I figured it out. The JSON was having issues with the “#” character. I just changed it to a text string that isn’t likely to be used on purpose and it worked! Thanks for helping to troubleshoot this one with me Pieter! You were very responsive!

  2. I’m hung up on the GET request. I’ve included my subsite but am getting an “Unauthorized error”. I own this subsite, and have reconnected SharePoint with my credentials, but not getting anywhere.

  3. I simply get “Invalid Request”:

    “errors”: [
    “-1”,
    “Microsoft.SharePoint.Client.InvalidClientQueryException”
    ]
    }

    Are you using the etag in the If-Match header? Or which compose do you have there?

      1. Okay – so the problem I have is when adding the compose, the only dynamic content option from the Send to HTTPS Get action is “body”. I do not have a dynamic option for ‘body/d/__metadata/etag’ of for type…just “body”.

  4. Hello Pieter, Just made use of your perfect solution, very well explained. Thank you, the HTTP request is not that easy to set up myself. Gr. Lex

  5. I did as explained in your article, the flow runs successfully, everything seems to be right (only that I can’t find “FileLeafRef” in the output, is that normal?), but the name doesn’t get changed!
    In the HTTP Request to get Type and Etag, that’s what I get:
    “etag”: “\”3\””,
    “type”: “SP.Data.LitigesItem”
    In the HTTP Request to change the name, here’s what the If-Match header gets:
    “3”
    Here’s the input:
    {
    “__metadata”: { “type”: “SP.Data.LitigesItem”
    },
    “Title”: “TestDaniel8”,
    “FileLeafRef”: “L1062 – TestDaniel8”
    }
    There’s a lot of content in the output so I will not paste it to not make an unreasonably long comment, but I couldn’t find “FileLeafRef” in there.

  6. Hi Pieter,
    Sorry, I can’t read! Somehow I thought that part about the root site collection was about whether it was applied to a subsite or not.
    I changed the URI accordingly, but now the flow fails at the first HTTP Request because it can’t find it (404 error).
    That was the URI when it was successful (but not working):
    _api/web/GetFolderByServerRelativeUrl(‘Litiges/@{outputs(‘Mettre_à_jour_les_propriétés_du_fichier_-_Litiges_(création)’)?[‘body/{Name}’]}’)/ListItemAllFields
    I didn’t put a slash before the library name as it was failing with a slash before.
    Now, I tried the URI for a site that is not the root site, both with a slash and without, in both case it’s a 404 error. There’s the URI, with the website name censored (in all caps) because all the info is highly confidential:
    /sites/SITE/_api/web/GetFolderByServerRelativeUrl(‘/sites/SITE/Litiges/@{outputs(‘Mettre_à_jour_les_propriétés_du_fichier_-_Litiges_(création)’)?[‘body/{Name}’]}’)/ListItemAllFields
    Interestingly, in the output, the “sites/SITE” part appears two time, which explains why it doesn’t find it:
    “source”:”https://mycomp.sharepoint.com/sites/SITE/sites/SITE/_api/web/GetFolderByServerRelativeUrl(‘SITE/Litiges/TestDaniel2’)/ListItemAllFields”
    Thanks for helping me,
    Daniel

  7. Thank you a lot Pieter! During our chat I sent you screenshots and you found that my second HTTP request, the one to rename the folder, had “GET” instead of “POST” as the problem. Once I got the URI right (“_api/web/GetFolderByServerRelativeUrl(‘/sites/anysite/Shared Documents/testfolder’)/ListItemAllFields”) and I used the method “POST”, it worked!

  8. Hello,

    I am getting the same error
    Body:
    {
    “message”: “Solicitud no válida.\r\nclientRequestId: fd19f4a0-63c9-4012-ba80-72c10591abfd\r\nserviceRequestId: 026eb6a0-b0c0-3000-973a-9f3350ce4b44”,
    “status”: 400,
    “source”: “https://arauco.sharepoint.com/sites/CL-Conflictos.Arauco/JuiciosCiviles/_api/web/GetFolderByServerRelativeUrl(‘/sites/CL-Conflictos.Arauco/JuiciosCiviles//Documentos%20compartidos/rgrtert’)/ListItemAllFields”,
    “errors”: [
    “-1”,
    “Microsoft.SharePoint.Client.InvalidClientQueryException”
    ]
    }

  9. Hi Pieter,

    Thank you for sharing this, this post gave me the right information to get it done on my side

    Sébastien

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading