How to Replace a File in SharePoint using Power Automate?

A few days ago, I worked with a client who wanted me to design a Project Management system using SharePoint lists, libraries, and site pages.

The client will receive the project-related files as attachments in their Outlook, which will be saved to the SharePoint document library.

The requirement here was: whenever a new email with an attachment is received, if the file already exists in the SharePoint document library, it should be replaced.

In this step-by-step tutorial, I’ll show you how to quickly set up a flow that automatically replaces files in your SharePoint library using Power Automate.

Replace a File in SharePoint using Power Automate

I have a SharePoint document library named [Project Documents] that consists of various types of documents, including .pdf, .xlsx, .doc, and .ppt, related to projects.

Replace SharePoint File using Power Automate

Here, I wanted to replace a file present in the document library that already exists with the email attachment.

Suppose, for example, in the above-mentioned document library, I have a ‘Project Details.xlsx’ File. If I receive an email with ‘Project Details.xlsx’ attachments, then I want to replace the SharePoint file with the attachment received in the email.

You can see the Email Below:

Replace SharePoint Library File in Power Automate

To achieve this, I will create an Automated cloud flow in Power Automate that will be triggered based on the provided trigger.

To do this, see the steps mentioned below:

  1. On the Power Automate Home page, select Automated cloud flow. If you’re a beginner, check out creating an Automated cloud flow in Power Automate.
  2. Then, choose to add ‘When a new email arrives (V3)’ trigger. Click on the Advanced parameters drop-down and select the ‘Only with Attachments‘ option.
    • Only with Attachments: Select the ‘Yes’ option.
Power Automate replace SharePoint file
  1. To get all the files present in the SharePoint document library, add the ‘Get files (properties only)‘ action. Provide Site Address and Library Name.
Replace SharePoint file Power Automate
  1. Next, I will add a ‘Get Attachment(V2)’ to get the attachment content. Set the parameters below.
    • Message Id: Select Message Id from the dynamic content of the trigger.
    • Attachment Id: Add Attachment Id here from dynamic content.

Then, it will automatically add a ‘For each’ loop by taking the Attachments value.

Replace SharePoint file in Power Automate

By now, Power Automate will retrieve all the files from the SharePoint library and attachments from the Email. Let’s check if the email attachment already exists in SharePoint.

  1. Following the previous action, add a Condition control to check that the attachment name equals any of the files in the SharePoint library. Set the parameters as given in the table below.
Choose ValueOperatorChoose Value
Attachment Name
@{outputs(‘Get_Attachment_(V2)’)?[‘body/name’]}
Is equal toFile Name with extension
@{items(‘For_each_1’)?[‘{FilenameWithExtension}’]}

A loop will be added above to retrieve the body/values of the Get Files action.

Replace SharePoint document library file in Power Automate
  1. Under the True branch, add the ‘Delete file’ action to delete the existing file in the SharePoint document library. Provide the necessary parameters, such as Site Address and File Identifier.
    • File Identifier: Select the Identifier from the Get files dynamic content.
Replace a File in SharePoint using Power Automate
  1. Next, add a ‘Create file’ action to replace the deleted file with an email attachment. Set the required parameters presented below.
    • Site Address: Select the specific SharePoint site.
    • Folder Path: Choose the document library from the open folder icon.
    • File Name: Provide the file name from the email attachment.
    • File Content: Choose Content Bytes dynamic content from Get attachment(V2).
Replace a File in SharePoint Power Automate

The flow is completed now. Save the flow by clicking on the Save icon. The flow will be triggered when a new email arrives in Outlook.

Replace a File in SharePoint with attachment Power Automate
  1. When the flow executes successfully, you can see the output. The file will be replaced with the attachment email. Refer to the image presented below.
Power Automate Replace a File in SharePoint with attachment
Power Automate Replace SharePoint file with attachment

This is how to replace a file in SharePoint using Power Automate.

Have you found this tutorial helpful? Do mention your suggestions in the comment section.

You may also like the following tutorials:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…