How to Rename a SharePoint Folder or File using REST API with Power Automate

Last Updated:

Short notes on how rename a SharePoint folder or file using the SharePoint REST API.

1. How to Rename a Folder 📁

In the example below I have a document library called “Another Demo Library”. There is a folder called “This is the folder” which has the item ID of 72 that I want to rename. The flow will rename this folder (item ID 72) to “Renamed Folder”:

In the example below I have a document library called "Another Demo Library". There is a folder called "This is the folder" which has the item ID of 72 that I want to rename. The flow will rename this folder (item ID 72) to "Renamed Folder":

Here is the code used in the API request using the POST action:

USAGE

Method: POST

Uri:

_api/web/lists/GetbyTitle('<Document Library Name'>)/items(<item-ID>)/validateUpdateListItem

Body:
{
  "formValues": [
    {
      "FieldName": "FileLeafRef",
      "FieldValue": "<new-folder-name>"
    }
  ]
}

Here is the example code for the API request using the POST action:

_api/web/lists/GetbyTitle('Another Demo Library')/items(72)/validateUpdateListItem

{
  "formValues": [
    {
      "FieldName": "FileLeafRef",
      "FieldValue": "Renamed Folder"
    }
  ]
} 

And here is the HTTP request used in the flow:

This is the HTTP request to rename a folder
This is the HTTP request to rename a folder

Here is the folder being renamed to “Renamed Folder”

Here is the folder being renamed to "Renamed Folder"

2. How to Rename a File 📄

Here is one method to rename a file in three steps:

  1. Check Out file
  2. Rename the File
  3. Check In the File

You can also simply rename the file without using the Check In and Check Out actions. You’ll see this in Example 2: Renaming a File.

How does it work using the Check In and Check Out actions?

Flow to check-out the file, rename the file and then check-in the file.
Click above to see larger image

We can use the SharePoint item ID to check-out the file, rename the file and then check-in the file.

We can use the SharePoint item ID to check-out the file, rename the file and then check-in the file.
Flow to rename a file using the check out/ check in actions.
USAGE

Method: POST

Uri:

_api/web/lists/GetbyTitle('<Document Library Name'>)/items(<item ID>)/validateUpdateListItem

Body:

{
  "formValues": [
    {
      "FieldName": "FileLeafRef",
      "FieldValue": "<filename>"
    }
  ]
}
_api/web/lists/GetbyTitle('Documents')/items(8)/validateUpdateListItem

Body:
{
  "formValues": [
    {
      "FieldName": "FileLeafRef",
      "FieldValue": "TEST123ABC.docx"
    }
  ]
}

Example 1: Renaming a File with Check In and Check Out actions

When a file is created (properties only) trigger.
Power Automate When a file is created trigger

Get file content using path
Power automate Get file content using path

In this step I want to save the file to OneDrive before it gets renamed:

Create file in OneDrive for Business
Power automate OneDrive for Business Create file action
Check out file
Power automate check out file
Use a HTTP request to SharePoint to rename file
Power automate Send an HTTP request to SharePoint. I’m using the current date and time as the filename as you can see in the FieldValue.
Check in file
Power automate check in file

Sample runtime output:

Sample runtime output of the flow
Power automate sample runtime output

Example 2: Renaming a File

You may also be able to simply rename the file without using document version control (Check-Out, Check-In):

Renaming a file using the HTTP action

For Further reading

For various other ways to rename a file using Power Automate, see the following post: Power Automate Rename File


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

Website Built with WordPress.com.

Up ↑

%d bloggers like this: