cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

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

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

@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

 

View solution in original post

7 REPLIES 7

Kindly F5

@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

 

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!

 

 

@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.

@creativeopinion 


Thanks, it worked!

 

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

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.

@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?

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages