Power Automate: Archive list item attachments to folders in a document library after x days

The purpose of this sample solution is to archive multiple attachments from list items at 10 days after the date set in the list item’s CompletedDate column. It also allows for deletion of the original attachments from the list items once archived. The flow adds metadate to the document library folders and files to indicate the title of the original list item as well as the ID of the original list item.

Let’s dive in!

Example List:

Example list with multiple attachments each item

Example Archival Library:

Archival Library – Folders
Archival Library – Files

1.

On a daily (your preference) basis, get all list items that are 10 days past CompletedDate and have not been marked as processed (AttachmentsMoved).

  1. Set up your flow as a recurring flow.  I have mine running every 1 Day
  2. Add the step: Get Items to get items from your list. 
    • Filter Query:
CompletedDate le 'formatDateTime(addDays(utcNow(),-10),'yyyy-MM-dd')' and AttachmentsMoved eq 'False'

Note: the items I built using the Expression builder are highlighted

3. Finally, add an “Apply to Each” step.  I have renamed mine to “Apply to each List Item” for clarity.

Recurrence – Get Items (list) – Apply to each

2.

Under Apply to each List Item we will get all attachments for each item

We are then creating a folder based on the title of the list item, updating the folder metadata to link it back to the item, and updating the original list item to mark it as processed (AttachmentsMoved = Yes)

  1. Get Attachments:
    • ID: ID from “Get Items”
  2. Create new Folder:
    • Folder Path: ReportTitle from “Get Items”
  3. Update File Properties (Here we are adding some metadate to the new folder we created):
    • ID: ID from “Create New Folder”
    • ListItemReportTitle: ReportTitle from “Get Items”
    • ListItemID: ID from “Get Items”
  4. Update Item (Here we are updating the original list item to mark it as processed (AttachmentsMoved)
    • ID from “Get Items”
    • Update AttachmentsMoved Yes/No column to Yes
Apply to each list item – Get Attachments – Create New Folder – Update File Properties – Update Item

3.

I have renamed “Apply to each” to “Apply to each Attachment”.

Here, we will process each attachment for the current list item being processed.

We get the attachment content (the file), we create the file in the library, and we add metadata to the library file to link it to the original list item.

Under Apply to each Attachment:

  1. Apply to Each Attachment -> Select an output from previous step: Body from “Get Attachments”
  2. Get Attachment Content:
    • ID: ID from “Get Items”
    • File Identifier: ID from “Get Attachments”
  3. Create File (here we are adding the file to the document library)
    • Folder Path -> Title from “Create New Folder”
    • File Name: DisplayName from “Get Attachments”
    • File Content: Body from “Get Attachment Content”
  4. Update File Properties (Here we are adding metadate to the document library to indicate the source report for the file
    • ID: ItemID from “Create File”
    • ListItemReportTitle: ReportTitle from “Get Items”
    • ListItemID: ID from “Get Items”
Apply to each Attachment – Get Attachment Content – Create File – Update File Properties

4.

OPTIONAL: Here, we are removing the archived attachments from the list item. Since they now exist in the document library and reference the original list item, there is no need to duplicate the files.

  1. Delete Attachment
    • ID: ID from “Get Items”
    • File Identifier: ID from “Get Attachments”
Delete Attachment (from original list item)