Skip to main content

Notifications

Community site session details

Community site session details

Session Id : Ga86neswkw1hlhrHEAB2md
Power Automate - General Discussion
Suggested answer

How to Replace an Existing SharePoint File via Power Automate Using PowerApps ?

Like (0) ShareShare
ReportReport
Posted on 31 Jul 2024 11:41:05 by
 

Hi everyone,

I'm working on a project where I need to replace an existing file in a SharePoint library. The file to be replaced is selected using a PowerApps app, which returns the name of the file. I'm looking to automate this process using Power Automate, but I'm unsure of the best approach and actions to use.

Here are the details:

  • PowerApps: The app allows users to select a file from the SharePoint library and returns the name of the file that needs to be replaced.
  • Requirement: If the file exists in the SharePoint library, replace it with a new version (I need help for this). If it doesn't exist, create a new file (I already made this part)
  • Desired Behavior: I would like to use a similar behavior to the "Replace" button in SharePoint, which replaces an existing file when you upload a new file with the same name. It will not erase all version history.

Could anyone  guide me on the actions and logic needed in Power Automate to achieve this? Any examples or detailed steps would be greatly appreciated.

Thank you in advance for your help!

 
 
 
 
 
Categories:
  • Suggested answer
    Greatmohan1 Profile Picture
    2 on 14 Nov 2024 at 17:25:00
    How to Replace an Existing SharePoint File via Power Automate Using PowerApps ?
    Hi,
     
    I'm not expert but I have found this to be a really challenging issue, especially if you need to maintain a unique Link (e.g. if you have Unique Document ID turned on), and if you want to maintain the document metadata. 
     
    In my solution, I am using a Powerapp to provide a document management function using Sharepoint document libraries and had to use several flows to get it working.
     
    I didn't find the answer in any one forum, but have managed to cobble together a working process so thought I'd share it:
     
    1. Change the file name in Sharepoint 
     
    You can only effectively replace a file if the file name is identical. If you try to upload a file with a different name, it will create a separate file rather than replacing the one you want to change. If you delete the file and create a new one, then the old ID and metadata are lost...
     
    If the file you want to upload has a different filename (e.g. if original file is Filename1 v1.pdf and the new file is Filename v2.pdf), you will need to change the file name first. 
     
    To do that, I first used Matthew Devaney's smart suggestion for how to create a blank attachments card which you can use to select a file to upload in to the app: https://www.matthewdevaney.com/power-apps-easiest-way-to-upload-files-to-a-sharepoint-document-library/
     
    I then set a check to see if the uploaded file name matched the existing file name.
     
    If the name is different then I used a flow to change the file name. See Sumit Kanchan's helpful guide for details: https://www.sharepointwidgets.com/2022/08/power-automate-rename-file-in.html
     
    NB: I had to use a 4 second timer and a 'pls wait' message before refreshing the data source so that the app would know that the two now had the same name.
     
    2. Upload the file
     
    Once the file names matched, then I went back to Matthew's guide to creating the flow to upload the file.
     
    There was one additional thing I needed to do. M365 Tech help's youtube video gave me the answer: https://www.youtube.com/watch?v=9kIydG85Pcc
     
    You need to disable the content transfer slider on the 'create file' step of your flow to enable overwrite of an existing file.
     
     
    Hey presto - you have now got a new file with the same meta data and the same document ID link. NB: If using Document ID - There is a delay of about 5-10 minutes for the Document ID link to update. You will see a '404 error' if you try to use the link before the update is complete. I had to add a user message to this effect.
     
    Hope that helps,
    Jem
     
     
  • Suggested answer
    AnthonyAmador Profile Picture
    2,546 Super User 2025 Season 1 on 31 Jul 2024 at 18:36:41
    How to Replace an Existing SharePoint File via Power Automate Using PowerApps ?
    Hi,

    Only the ‘Copy File’ and ‘Move File’ actions in the SharePoint connector contain the option ‘If another file is already there’. Therefore, you would have to create the logic for that. Since you already have the name of the file, you can use the ‘Get Files (Properties Only)’ action to get all the files in the folder, then compare against the name you have from Power Apps. If the file is there, then delete it and create a new one with the content from Power Apps.

    It should look like this.
     


    Hope this helps.
    Anthony. 
     
    If my reply answers your question, please like and mark it as verified. This helps me stay motivated to assist others and makes it easier for users with similar questions to find the answer. Thanks!
    Feel free to connect with me on LinkedIn!
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Automate - General Discussion

#1
stampcoin Profile Picture

stampcoin 29

#2
Pstork1 Profile Picture

Pstork1 22 Most Valuable Professional

#3
David_MA Profile Picture

David_MA 20 Super User 2025 Season 1

Overall leaderboard
Loading started