Learn how to copy a file from one library to another using Power Automate, with the complete version history of the file in SharePoint
The challenge
When copying a file from one library/site to another you won’t get the complete version history in SharePoint.
Using a HTTP request called CreateCopyJobs we are able to copy the file containing the version history.
The solutions
Step 1
Create a new Instant cloud flow and select the trigger For a selected file
Step 2
Add the action Get file properties and insert the ID from your trigger output
Step 3
Add the action Send an HTTP request to SharePoint with the following options
Method
POST
Uri
\_api/site/CreateCopyJobs
Headers
Accept
application/json; odata=nometadata
Content-Type
application/json; odata=verbose
Body
{
"exportObjectUris": [
"@{outputs('Get_file_properties')?['body/{Link}']}"
],
"destinationUri": "https://XYZ.sharepoint.com/sites/Blog/Copy%20library",
"options": {
"IgnoreVersionHistory": false,
"IsMoveMode": false,
"MoveButKeepSource": true,
"AllowSchemaMismatch": true,
"NameConflictBehavior": 1,
"AllowSmallerVersionLimitOnDestination": true,
"BypassSharedLock": true
}
}
Replace destinationUri with your destination library. Be carefull not to introduce an / in the end of your URL
Your HTTP request should look like this
Start your flow
This specific flow will be triggered for a selected file, so let’s add a button to our library.
Create a new column called CopyFile and add the following JSON to the column.
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"customRowAction": {
"action": "executeFlow",
"actionParams": "{\"id\":\"FLOWIDENTIFIER\", \"headerText\":\"Copy file\",\"runFlowButtonText\":\"Copy file\"}"
},
"style": {
"display": "flex",
"align-items": "center",
"margin-top": "2px",
"margin-bottom": "2px"
},
"children": [
{
"elmType": "a",
"attributes": {
"class": "ms-bgColor-themePrimary ms-bgColor-themeDarker--hover"
},
"style": {
"cursor": "pointer",
"height": "30px",
"text-decoration": "none",
"display": "flex",
"align-items": "center",
"text-align": "center",
"justify-content": "center",
"margin": "5px",
"color": "#fff",
"border-radius": "3px"
},
"children": [
{
"elmType": "span",
"attributes": {
"iconName": "Copy",
"class": "ms-fontSize-16 ms-fontWeight-regular",
"title": "Details"
},
"style": {
"color": "#fff",
"flex": "none",
"margin-right": "5px",
"margin-left": "8px"
}
},
{
"elmType": "span",
"style": {
"margin-right": "8px"
},
"txtContent": "Copy file",
"attributes": {
"class": "ms-fontWeight-bold"
}
}
]
}
]
}
Replace FLOWIDENTIFIER with your own flow identifier.
Getting the flow identifier
You can get the flow identifier from the Export button in the top ribbon
Be carefull that you don’t remove the \ when inserting your identifier
The code should look like this
"actionParams": "{\"id\":\"v1/Default-efadct70-cxsa-45b1-a688-8442bf7ce56c/7839e3b9-62c8-4130-44b6-a1e696ca4999\", \"headerText\":\"Copy file\",\"runFlowButtonText\":\"Copy file\"}"
We can now copy a file from one library to another using Power Automate with full version history in SharePoint
See also
- Create a searchable dictionary with PnP Modern Search in SharePoint
- Create a custom button to open files in a SharePoint Library
- How to display file thumbnails on hover with JSON formatting in SharePoint
- Refresh SharePoint list data fast in Power BI
- Create an interactive "review" button in SharePoint