cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Flow for Archiving Attachments to OneDrive

I'm trying to build a Flow that will Archive attachments with a specific name that are older than a month into a specific OneDrive For Business folder. Not sure were to start. Roughly I thought this would work:

 

Get Email ---> Condition: If Received Date is Older than a Month --> Yes --> Create File ---> Delete Email

1 ACCEPTED SOLUTION

Accepted Solutions

 

length(body('Get_emails'))

 

I think your final solution might look like this:

 

2018-10-09 09_18_38-Edit your flow _ Microsoft Flow.png

 

Be sure to include the processing of the "Get emails" action inside the "Do until".

 

View solution in original post

14 REPLIES 14
degvalentine
Kudo Collector
Kudo Collector

I recently did something similar! Assuming you're using the "Get Emails" action of the Office 365 Outlook connector, set it up like this:

 

2018-10-05 16_53_17-Edit your flow _ Microsoft Flow.png

 

  • Be sure to select "Yes" for "Include Attachments".
  • You can skip the Condition by using the "Search Query" as I demonstrated. Date ranges are expressed by using 2 dots as a delimiter ("..").
  • You can then iterate over body('Get_emails').

Here's the format of the email data you'll get back:

 

{
  "From": "...",
  "To": "...", // addesses delimited by ";"
  "Cc": "...",
  "Bcc": null,
  "Subject": "...",
  "Body": "...", // html content
  "Importance": "Normal",
  "BodyPreview": "...",
  "HasAttachment": false,
  "Id": "...",
  "ConversationId": "...",
  "DateTimeReceived": "...", // timestamp
  "IsRead": true,
  "Attachments": [
    {
      "Id": "...",
      "Name": "...",
      "ContentBytes": "...",
      "ContentType": "...",
      "Size": ...
    }
  ],
  "IsHtml": true
}

 

Another foreach to iterate item()['Attachments'] and you're there.

 

2018-10-05 17_05_02-Edit your flow _ Microsoft Flow.png

Anonymous
Not applicable

That's great. I'll try it out. Is there no way to do it so the search query date is dynamic? "Every 30 days" or something?

Of course B)

 

2018-10-08 14_39_39-Create your flow _ Microsoft Flow.png

Anonymous
Not applicable

Does this make sense? I'm basically trying to save the attachment with the name as the subject and time received, and File content is attachments content. I don't understand why the "apply to each" happens?

image.png

 

image.png

 

 

The first "Apply to each" is needed to iterate over the emails.

 

The second "Apply to each" is needed to iterate over each email's attachments; you can see in the sample data I provided (or in the data you're now seeing) that it's an array.

 

Also, my example with the "Past time" output in the email filter wasn't complete. Don't forget to use the date range syntax like I showed in my first reply, using the "..". Something like this: "received:2000-01-01..[PAST-TIME]".

Anonymous
Not applicable

Perfect. I got it working. I'm assuming since this only runs for the first 25 emails within the search result I should make the reoccurrence happen fairly frequent? Any ideas on how frequent I should run this to make it work properly?

Each Flow can run for up to 30 days. No need to burn up your monthly Flow execution limit (depending on your plan).

 

Try using a Do Until to execute until no more results are returned.

Anonymous
Not applicable

I assume it would be like thi? What would I choose for the value though? 

 

image.png

 

length(body('Get_emails'))

 

I think your final solution might look like this:

 

2018-10-09 09_18_38-Edit your flow _ Microsoft Flow.png

 

Be sure to include the processing of the "Get emails" action inside the "Do until".

 

View solution in original post

Also, be sure to extend the limit of your loop. A "Do until" requires an execution limit in time or iterations. 5000 is the max allowed iteration count. So, erase the default time limit and increase the "Count".

 

2018-10-09 09_24_49-Edit your flow _ Microsoft Flow.png

Anonymous
Not applicable

Thank you for your help! I just ran my flow and it now works perfectly.

Awesome! Congrats on turning an intractable manpower problem into a pretty Flow.

Anonymous
Not applicable

One last question! 😄 So, the flow only runs for 10 minutes, I update it to run for 30 days (A bit overkill). I'm sure there's a way to just run it until the "Do Until" realizes there are no more emails, right?

 

EDIT: Just saw that you had the count set to 5000. Is that the best way to do it?

Good news! You already have your Do Until loop set to run until there are no more emails.

That's the purpose of this part:

2018-10-10 13_08_23-Solved_ Flow for Archiving Attachments to OneDrive - Power platform Community.png

When your "Get emails" action returns an empty set, the loop will stop.

 

Seems the Do Until is one of the most confusing actions, based on the number of community topics related to it. From a dev's perspective, it's a "do while" loop with an inverted end condition. Weird. <hint>I'm sure the Flow team will improve it in the future.</hint>

 

The Do Until also has a "limit" in either Count, Timeout, or both. Each limit has a max value - for Count, it's 5000, for Timeout, it's 30 days. If you expect more than 125k emails (5000 x 25) each month to process, stick with your 30 day limit.

Helpful resources

Announcements
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (2,302)