Codementor Events

Rename File in SharePoint Library with Power Automate

Published Jul 17, 2024

While I am trying to create an export to excel flow, I found out that we don’t have a direct action to rename a file in SharePoint document library. So lets rename a file using SharePoint HTTP Request action.

Above, we can see a pdf file in Documents library named as Orange.pdf. Now, lets rename this file to Green.pdf

We would be using “Send an HTTP request to SharePoint” action to achieve this.

Lets understand the parameters

Site Address: <Select your site>
Method: POST
Uri: _api/lists/GetByTitle('<Library Name>')/Items(<Item ID>)

I have hardcoded the item ID in the action, whereas we can get the item ID from relevant previous actions.

Headers:
Content-type: application/json;odata=verbose
IF-MATCH: *
X-HTTP-METHOD: PATCH
Body:
{'__metadata':
{'type':'SP.Data.Shared_x0020_DocumentsItem'},
'FileLeafRef':'Green'}

Above are the two crucial parameters for this action. Lets try to understand themFirstly, “FileLeafRef” is the parameter where we want to give new file name which in our case is “Green”
Secondly, “type” which is a little complex.

{‘type’:’SP.Data.Shared_x0020_DocumentsItem’} — For ‘type’ we can get the value by following format
SP.Data.<DocumentLibraryInternalName>Item

Few examples for better understanding:

External name: Documents
Internal name: Shared Documents
Then the value would be SP.Data.Shared_x0020_DocumentsItem (Space is replaced with “x0020”)

External name: InvoicesDocs
Internal name: InvoicesDocs
Then the value would be SP.Data.InvoicesDocsItem

Also alternatively, we can use the GET method of SharePoint HTTP Action to get the above value.

When we use this action, the type would be

  Body:
{'__metadata':
{'type':'@{outputs('HTTP_request_to_SharePoint_to_find_type')?['body']?['d']?['ListItemEntityTypeFullName']}'},
'FileLeafRef':'Green'}
Discover and read more posts from Vijaya Bhaskar
get started