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 1

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 2

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 3

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 4

And the title has been set to New Name

Rename SharePoint folders using Power Automate in 2 easy steps 5

And our mission to rename SharePoint folders has succeeded.

Leave a Reply

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

%d bloggers like this: