Power Automate to copy files between SharePoint Online sites
When using SharePoint Online, an issue for which there is no graphical solution in the SharePoint interface, is being able to copy files from one site to another.
Sure, there are options such as:
- using ShareGate, but that software costs money
- download the file from SharePoint online and then upload the file to the target location
But wouldn’t it be nice if there was a simple solution allowing users to just being able to click on the 3-dot menu “…” next to the file, select “automate” from the context menu, and selecting the name of the workflow to run which does the job for you?
Yes, I thought so too. Hence this post. 🙂
This post will detail a flow you can create a Power Automate to copy files between SharePoint Online sites. This post assumes some basic Power Automate familiarity.
Ready? Here we go!
For those who want to skip the foreplay and see the overall flow, here it is:
The important details
You create a new flow by:
- going to the “Automate” menu in SharePoint for the document library where the files reside
- select “Power Automate” from the dropdown
- select the “Complete a custom action for the selected file” template. You may need to select “show more” if it’s not immediately available.
Once complete, here is each action step in the workflow:
1) “For the selected file”
- Select from the dropdown the site under which this flow will reside
- Library Name, counter-intuitively, is the guid of the document library. This can be gathered from the browser’s URL when clicking on the “settings” for the document library.
- “Add an input” called “targetSiteURL” which will prompt the user for the URL of the site the file is to be copied to.
2) “TODO: – set CONST – Current Site Address”
This will set a string that is the URL of the current site. This will be used to elsewhere in the workflow so when the flow is deployed to different sites, it only has to be updated here and Action #1 as described above. Unfortunately, for Power Automate there is no easy way to dynamically obtain the current site address context like there is within Nintex Automation Cloud. If you do know of a way, please let me know in the comments!
3) “TODO: – set CONST – Library Name”
Same reason as per #2 above, except this is the guid for the source document library. It will need to be updated if the workflow is deployed to a different site.
4) “TODO: – set CONST – Site Pages Path”
A string constant that’s the relative URL for the “site pages” document library. Basically, it should be the same on any SharePoint Online site and shouldn’t need updating.
5) “Get file properties”
This gets the file information from the document library specified in #3 above, from the site in our constant from #2 above. The “ID” can be selected by clicking in the ID field and within the “Dynamic content”, looking for the “ID File Identifier”. This should be the unique SharePoint Online ID for the file that is going to be copied, determined at runtime.
6) “Initialize variable – File Path”
To be able to obtain the name of the file selected, the entire URL needs to be captured for parsing. This step captures that into the variable called “filePath”.
7) “Get page file name”
This is a “Compose Data operation” action and will be used to obtain the file name from the URL, and store it in the variable created in #6 above.
To do so, click on the “Inputs” field, select “Expression”, and add the following:
last(split(variables(‘filePath’),’/’))
8) “Copy File”
Will copy the file to the specified target site. Just plop in the variables as shown with two caveats:
- don’t miss the “/” character in the “File to copy” field
- select the appropriate action you would like from the “if another file is already there” dropdown option
That’s all there is to using Power Automate to copy files between SharePoint Online sites
Simple, right?
Happy Power Automate building!
Please leave comments if you have any Power Automate tricks or tips of your own to share!
