In this Power Automate tutorial, we will see how to check if a file exists in the SharePoint document library in Power Automate.
Scenario:
We have a SharePoint Document Library called ‘Attachments‘ like below. When a new email arrived with an attachment. We will get the attachments, and then we will check if the file exists in the SharePoint folder, then we will delete the file and create a new file. If not exists, then it will directly create the file.
Below you can see the Attachment Library in SharePoint.
Now let’s create the flow to check if a file exists using Power Automate.
How to check if a file exists using Power Automate
Here we will see an example of how to check if a file exists using Power Automate.
Before creating the flow, create a document library in your SharePoint Online site, where you will save the attachments.
Step 1: Log in to Power Automate, then click on the +Create icon -> select Automated Cloud flow.
Then provide the flow name, and select When a new email arrives(V3) action. Click on Create.
Now, you can see that When a new email arrives action is added to the flow page. Then provide the below information:
- Folder: Select Inbox, where you will receive an email.
Step 2: Next click on the +New step -> select the ‘Get attachment (V2)‘ action. Then provide the below information:
- Message-Id: Select the Message-Id from the dynamic content.
- Attachment Id: Select the Attachment Id from the dynamic content. That will automatically add Apply to each action
Step 3: Next click on Add an action, and select the Get files (properties only) action. Then provide the below information:
- Site address: Select or Provide the SharePoint site address.
- Library name: Select or provide the SharePoint Library.
Step 4: Now we will check the file that exists in the folder, if exist then it will delete and create the file. Else it will only create a file.
So, click on the Add an action -> select Condition action. Then provide the below information:
- Choose a value: Select the Name as the value
- Operator: Select the operator as is equal to
- Choose a value: Provide the Name- Get file properties from dynamic content
If the condition is true, then in the If yes condition, click on Add an action -> select Delete file action. Then provide the below information:
- Site address: Select or provide the SharePoint site address.
- File identifier: Select the Identifier from the dynamic content. This will automatically add Apply to each action.
To create the file, click on Add an action -> select Create file action. Then provide the below information:
- Site address: Select or Provide the SharePoint site address
- Folder path: Select the folder path.
- File name: select the File name from dynamic content.
- File content: Select the Content Bytes from dynamic content.
Now if the condition is false, then in If No part, click on Add an action -> select Create file action. Then provide the below information:
- Site address: Select or Provide the SharePoint site address
- Folder path: Select or provide the folder path
- File name: Provide the name from dynamic content
- File content: select Content Bytes from dynamic content.
Step 5: To run the flow click on Save and then click on the Test icon -> select Manually -> click on the Test button. Configure the connections -> click on Run flow -> click on Done.
You can send an email to the inbox of your Outlook, you can see your flow run successfully.
As our folder already contains a file, it will delete the file, and recreate the file in the folder.
Conclusion
In this Power Automate tutorial, we saw an example that will check if a file exists or not in a SharePoint folder or document library using Power Automate.
You may also like:
- Copy Data from One Excel to Another Excel in Power Automate
- Create SharePoint List Views using Power Automate
- Update SharePoint List Field with today’s date in Power Automate
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com