web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / New Mail to Shared Mai...
Power Automate
Unanswered

New Mail to Shared Mailbox = Update file on SharePoint Document Library

(0) ShareShare
ReportReport
Posted on by 47

Dear PowerAutomate Experts,


I've an issue. Basically - when new e-mail is sent to Shared Mailbox, workflow should create/update files in the folder in Document Library (by attachments from that e-mail). When an item exists with the same name in Document Library, old file should be moved to "Archive" folder (in that Folder), if there's no file with that name, it should create a new one.

 

I'm getting name of the folder from Subject from the e-mail - it's working. But I don't know how to get files from that Folder and then compare the names to update/create (mentioned above).

karik_0-1693063961151.png

 

I'd be grateful for any help/hints how can I achieve that?

 

 

Categories:
I have the same question (0)
  • karik Profile Picture
    47 on at

    Kindly F5

  • Verified answer
    creativeopinion Profile Picture
    10,502 Super User 2025 Season 2 on at

    @karik I would recommend that you create a flow using the When a new email arrives (V3) trigger instead while you build it out before using the When a new email arrives in a shared mailbox (V2) trigger. Especially if your Shared Mailbox is active.

     

    Tip: Rename your actions to keep your flow organized.

    When a New Email Arrives

    As mentioned above, use the When a new email arrives (V3) trigger while you test and build your flow. 

     

    Ensure that you are filtering out emails that Include Attachments. I would also recommend specifying a Subject Filter as well. This way you can ensure that only emails with the subject line Flow Test will trigger your flow. 

    creativeopinion_1-1693090983263.png

    Getting Attachments

    Next, you need to retrieve your attachments and the file names of each attachment so you can run a cross-check with the files in your doc library. 

    Note: Scope action is not necessary—though I use them in my flows so I can group actions together as well as collapse multiple actions so they don't take up too much vertical space. 

     

    creativeopinion_2-1693093494088.png

     

    Add a Compose action and insert the attachments dynamic content. This Compose action will output the array of attachments.

    creativeopinion_3-1693093676865.png

     

    Optional: Add another Compose action to return the count of attachments. 

    creativeopinion_6-1693093846398.png

     

    Use an Expression and insert the length() function. 

    creativeopinion_4-1693093768871.png

     

    Switch to the Dynamic content tab and Insert the outputs from the Compose action above. 

    creativeopinion_5-1693093792505.png

    Select the Attachments Names

    You need a way to cross-reference your attachments with the files on SP. You indicated that if they had the same name, you'd like to archive the file that already exists on SP. 

     

    Add a Select action and insert the Outputs from the Compose action that contains the Attachments. Click on the icon to switch from Map to text mode. 

    creativeopinion_9-1693094095427.png

    Insert an expression. If you look at the outputs of the Compose action that has the Attachments, the key for the Attachment Name is "name". 

    creativeopinion_10-1693094141664.png

    The expression to access content from an array looks like this.:

     item()?['key goes here']

    To access the file name from the Attachment Array (Compose action) your expression will look like this:

    item()?['name']

    The key must match the text in red between the double quotes.

     

    Run a Test.

     

    The outputs of the Select action should look like this. The Select action will collect the names of the attachments into an array. 

    creativeopinion_11-1693094336217.png

     

    Convert the Attachment Name Array into a String

    You'll be using a Filter Array action to cross check the file names on SP. For that, you'll need a string of attachment names.

     

    Add a Join action. Insert the Outputs from the Select action into the From field. Enter a single comma only to the Starts with field

    creativeopinion_12-1693094537769.png

     

    Run a test.

     

    The Outputs of the Join action should look like this:

    creativeopinion_13-1693094611445.png

    Get Files

    Add a Get Files (properties only) action to your flow. This action will return files AND folders. To filter out files only, use the Filter Query below.

     

    FSObjType eq 0

     

    This action will return all the files from your Document library. Depending on how many files you have, you may want to reduce that number further by adding additional filtering (if possible)

    creativeopinion_14-1693094659954.png

    Filtering SP Files

    If you are not familiar with the Filter Array action, I would recommend watching this portion of a YT Tutorial I recently uploaded that talks about how the Filter Array action works.

     

    Add a Filter Array action.

    • In the From field, insert the value dynamic content from the Get Files action. 
    • In the first value field insert the Outputs from the Join action.
    • In the second value field insert the File name with extension dynamic content from the Get Files action.

    creativeopinion_15-1693094931514.png

    Get Count of Filtered Items

    Add a Compose action to store the count of the items returned from the Filter Array action. Use the length() function and insert the body outputs dynamic content from the Filter Array action. 

    creativeopinion_16-1693095048076.png

     

    Run a test.

     

    Ideally, your test email should contain attachments with file names that already exist in your Doc library and ones that do not exist. 

    Apply to Each

    Your requirement was to archive any files that already exist on SharePoint. The Filter Array action has filtered out any files that have a matching file name to your attachments. 

    creativeopinion_0-1693151811042.png

    To loop though those files that have been returned from the Filter Array action, add an Apply to Each action and insert the body dynamic content from the Filter Array action. 

    creativeopinion_1-1693152063843.png

     

    Get the Filename and Identifier

    Add a Compose action to the Apply to Each action to return the Filename of the SP file currently being looped through. This is optional—however it will help you to troubleshoot if something isn't working in your flow.

     

    You will need to use an expression to access the content from the Filter Array outputs. If you look at the outputs of the Filter Array action you can access any of the values by using the key (red text between the double quote marks)

     

    I chose to use the Filename with Extension, you can use the Name if you prefer. It really doesn't matter. This is just to help you confirm what is happening in your flow, should it not work the way you are expecting it to. 

     

    The expression is the same as it was above to return the Attachment name. Start off with:

    item()?['']

    Put the key between the single quotes. In this case it would look like:

    item()?['{FilenameWithExtension}']

    creativeopinion_3-1693152511780.png

    creativeopinion_5-1693152790070.png

     

    In order to move a file with the Move File action, it requires you to specify a File to Move. The action expects the File Identifier.

     

    Just as you've done above. Insert a Compose action to return the Identifier using the same expression. Ensure you are including the curly brackets in your expression.

     

    It should look like this:

    item()?['{Identifier}']
     

    creativeopinion_2-1693152468282.png

    creativeopinion_6-1693152802003.png

    Your Apply to Each action should have both Compose actions nested inside of it.

    creativeopinion_4-1693152755532.png

    Run a test. Confirm that the Compose actions above are outputting the Filename and Identifier for each SP file that matches your attachment names.

     

    Move File

    Add a Move file action within the Apply to Each action. Select your Site Address and insert the Outputs from the Compose action that contain the Identifier into the File to Move field. 

     

    Select the Destination Site Address, Destination Folder and select the action you want it to take if a file already exists with the same name.

    creativeopinion_8-1693153081294.png

     

    Create File for Each Attachment

    If you want to create a file for every attachment, you need to do it outside of the Condition branches. The purpose of the Condition was to Move a SP file if it already exists. Regardless if the file exists or not, you still need to create a file. 

     

    Add an Apply to Each action to the flow (outside of the condition branches). Insert the Attachments dynamic content. This action will loop through each attachment (again, regardless if the SP file already exists). If I've missed something else in the requirements for your attachments let me know.

     

    Add a Create file action into the Apply to Each loop. 

    creativeopinion_9-1693153411574.png

     

    Hope this helps!

    If I helped you solve your problem—please mark my post as a solution ✅.
    Consider giving me a 👍 if you liked my response!

    👉Watch my tutorials on YouTube
    👉Tips and Tricks on TikTok

     

  • karik Profile Picture
    47 on at

    Hi @creativeopinion,


    Thanks, everything is working! I'm already here, and took a condition, when Compose Attachments [length] Output is greater than 0, we will move already existing file with same name to Archive folder and then add all attachments to the folder. If no, we will just add attachments to the folder. I don't know how to "select" which File we want to Move:

     

    karik_1-1693140554474.png


    Thank you in advance and I really appreciate your help!

     

     

  • creativeopinion Profile Picture
    10,502 Super User 2025 Season 2 on at

    @karik Please refer to the original post as I've added the rest of the instructions. I cover how to get the File Identifier required for the Move File action.

     

    However, the logic in your flow needs to be adjusted a bit. You only need one Apply to Each loop. Again, refer to my original post that contains the rest of the instructions.

  • karik Profile Picture
    47 on at

    @creativeopinion 


    Thanks, it worked!

     

    I really appreciate it, if I can buy you a coffee or something, let me know! 😉

  • karik Profile Picture
    47 on at

    Hello @creativeopinion ,

     

    Firstly thank you for your help with that case

     

    Unfortunately I gave bad requirements and I have a problem with one thing, maybe you can help me with that also. Everything is working when the name matches from e-mail attachment & file in DL. But in my scenario I want to have something like:

     

    1. I've Draft.docx file in Document Library

    2. I'm sending Draft_v1.docx to shared mailbox

    3. It's moving Draft.docx to another folder (Archive) and creating Draft_v1.docx in this location (because Draft_v1.docx contains Draft.docx). But if I want to send Draft_v3.docx, comparing with Draft_v2.docx, it's not working due to it's checking whole name (Draft_v3.docx does not contain Draft_v2.docx). Is there a possibility to do something like: "compare File name, but only BEFORE "_" sign? Or how can I achieve what I want? Last, but not least, it's important that I can send multiple files in attachment so I'd like to compare all of them (before "_" sign).

    Thank you in advance & let me know, if you've something like "buymeacoffee", because I want to say "thanks" somehow.

  • creativeopinion Profile Picture
    10,502 Super User 2025 Season 2 on at

    @karik  From the Filtering SP Files of the solution post, the Filter Array action is being used to compare the attachment names received from the email (first value field) against the name of the file in SharePoint (with the extension). 

    creativeopinion_0-1697683253137.png

     

    If you want to filter out the files that contain Draft (as per your example) you will need to use the Name dynamic content from the Get files (properties only) action in the second value field instead of the File name with extension dynamic content.

     

    Take a look at my example below. In the first Compose action I'm using the Name dynamic content, in the second Compose action I'm using the File name with extension dynamic content.

     

    Note: You do not need to add these Compose actions to your flow—I'm using them to illustrate my point.

    creativeopinion_1-1697683771622.png

    The output of the Name dynamic content is just the filename (no extension). 

     

    creativeopinion_2-1697683848224.png

     

    Use the Name dynamic content in your Filter Array action. This will check all attachment names to see if the file name (no extension) is contained in the Join action outputs (in the first value field).

     

    creativeopinion_4-1697684095085.png

     

    ----

     

    In your post above, you indicated that you are sending a file? I'm a bit confused (unless it's a typo). The original post was about checking attachments received. Can you pls clarify what you mean by needing to send attachments? I'm unclear on the workflow. Is this a new flow or the same flow that your original post was about?

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard