Rename a SharePoint file via REST
Quiz question
Why do you rename via FileLeafRef instead of copying?
Solution
- A) Copying is never possible in librariesWrong. Copying is possible, but generally creates a new item. Version history and metadata may be lost.
- B) FileLeafRef also changes the file extensionWrong. In the tip's example, changing the extension through FileLeafRef had no effect. Use MoveTo for that.
- C) ID, versions and metadata are keptCorrect. MERGE on FileLeafRef only changes the name. The item itself stays the same.
What it is about
The SharePoint connector has no rename action. Copying and deleting generally creates a new file with a new ID. Version history and metadata may be lost. A single call with "Send an HTTP request to SharePoint" is cleaner. You send a POST to the list item of the file and turn it into an update with the headers IF-MATCH: * and X-HTTP-Method: MERGE. The body contains only the FileLeafRef field with the new file name including the extension. With the odata=nometadata headers you don't need a __metadata entry. A 204 response means success. ID, versions, permissions and metadata are kept. In the tip's example, changing the extension this way had no effect. Use MoveTo for that.
The expression from the tip
X-HTTP-Method: MERGEWhen to use it, when not
Use it
For naming conventions, reference numbers in the file name or corrections to uploaded files.
Better not
When the file extension or the folder should change. MoveTo is the right call for that.
Most common mistake
Omitting the extension when renaming. Keep it in the new file name and check the name after the request.
License
Standard This technique uses standard connectors. Microsoft 365 usage rights cover standard connectors. Check the rights for your whole flow. Source on Microsoft Learn: SharePoint, Connector-Referenz. For your whole flow: License check.
Share
Short link: powerautomate.ninja/en/153
Source: PowerPlatformTip #153, Microsoft Learn: Working with the SharePoint Send HTTP Request flow action.
Prefer to continue with guidance? The Power Automate live course (in German) at the Power Platform Academy runs over four weeks. The next dates are not set yet, you can join the waiting list on the course page.