Rename SharePoint folders in Power Automate

Rename SharePoint folders using Power Automate in 2 easy steps

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.

10 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 refering 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!

Leave a Reply

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

%d bloggers like this: