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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Extract most recent fi...
Power Automate
Unanswered

Extract most recent file in Sharepoint subfolder, attach file to Outlook email

(0) ShareShare
ReportReport
Posted on by 6

I have a Sharepoint subfolder with Excel spreadsheets.

I would like to create a Power Automate Flow that extracts the most recently modified Excel spreadsheet from this subfolder, then attach that spreadsheet to an Outlook (V2) connector.

 

I  have already created a simple flow that extracts a file within a Sharepoint subfolder, then attaches that file to an email: 

knuckledown234_0-1748385302612.png

 

This is my Get File Content Using Path, showing it extracting a specific file:

knuckledown234_1-1748385394873.png

This is my Outlook Send an Email (V2) step where that file is attached:

knuckledown234_2-1748385496508.png

 

I am sharing this to show that I know how to attach a file to an email, and to show that I know how to find the file path for a Sharepoint file. This flow is part of my current work-around, which is to have a subfolder where I overwrite a file with the same username and upload the only file within that subfolder.

 

However, I would like help with a) extracting the most recently modified file from a subfolder with many files and b) attaching that file to an Outlook email. Here is my attempt at extracting the most recent file from a SHarepoint folder and attaching it to an Outlook email:

 


 

This is my List folder action:

This successfully returns information on both of the files in this subfolder:

{
    "statusCode"200,
    "headers": { ....
    }, "body": [ {  "Id": "....xlsx",
    "Name""....xlsx",
    "DisplayName""...xlsx",
    "Path""...xlsx",
    "LastModified""2025-05-01T06:30:03Z",
    "Size"43844,
    "MediaType""application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "IsFolder"false,
    "ETag""\"{...},1\"",
    "FileLocator""..."
, {    "Id": "....xlsx",
    "Name""....xlsx",
    "DisplayName""...xlsx",
    "Path""...xlsx",
    "LastModified""2025-05-01T06:30:03Z",
    "Size"43844,
    "MediaType""application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "IsFolder"false,
    "ETag""\"{...},1\"",
    "FileLocator""..."
}
    ]
}

 

 

Compose action

 

My compose action has this powerfx code in the Inputs: 

first(reverse(sort(outputs('List_folder')?['body'], 'LastModified')))
This outputs information on the most recent file:
{
    "Id": "....xlsx",
    "Name""....xlsx",
    "DisplayName""...xlsx",
    "Path""...xlsx",
    "LastModified""2025-05-01T06:30:03Z",
    "Size"43844,
    "MediaType""application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "IsFolder"false,
    "ETag""\"{...},1\"",
    "FileLocator""..."
}
 

This is my outlook (send an email (V2)) action:

This is th e code view of the Outlook action:

{
  "type": "OpenApiConnection",
  "inputs": {
    "parameters": {
      "emailMessage/To": "fakeemail@email.com",
      "emailMessage/Subject": "Subject text",
      "emailMessage/Body": "<p class=\"editor-paragraph\">Body text</p>",
      "emailMessage/Attachments": [
        {
          "Name": "my_file.xlsx",
          "ContentBytes": "@outputs('Compose')"
        }
      ],
      "emailMessage/Importance": "Normal"
    },
    "host": {
      "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
      "connection": "shared_office365",
      "operationId": "SendEmailV2"
    }
  },
  "runAfter": {
    "Compose": [
      "Succeeded"
    ]
  }
}

The email successfully sends, but the attachment gives me an error message saying that the file format or file extension is not valid:
 

 

How would you recommend I change my flow so I can successfully attach the most recently uploaded file? I have used the Outlook Send an Email (V2) attachment many times to email files to myself but I seem to be struggling with extracting the most recent file from a subfolder and attaching that file to an Outlook email.

 

I would prefer to not use Premium connectors.

 

Categories:
I have the same question (0)
  • knuckledown123 Profile Picture
    6 on at
    I think I figured out to do this. I would be open to anyone who has feedback or better suggestions.
     
    I also obtained the powerfx code I also first(reverse(sort(outputs(...)))) from somewhere on this forum, but searching for the code is not turning up results. Thank you to who posted it!
     
     
     
    The first compose action has this code: 
    first(reverse(sort(outputs('List_files_in_folder')?['body'], 'LastModified')))
    This extracts the JSON for the most recently modified file in the folder's listed files.
     
    The compose action "Obtain most recent file path" has this code to extract the file path for the most recent file. I had the option to select this as a dynamic expression:
    outputs('Compose_action:_Obtain_the_most_recent_file_in_folder')?['Path']
     
    The compose action "Obtain most recent filename" has this code to extract the file name for the most recent file:
    outputs('Compose_action:_Obtain_the_most_recent_file_in_folder')?['Name']
     
    Next I used a Sharepoint action Get File Content. For the "File Path" blank, I am using the output from the second Compose action:
     
    Finally I used an Outlook Send an email (V2) action. The File Content is the output of the Get file content using extracted path action. The Name of the file will be the output of the third compose action, which extracts the file name.
     
     
     
     
     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 468

#2
Haque Profile Picture

Haque 370

#3
Valantis Profile Picture

Valantis 354

Last 30 days Overall leaderboard