Power Apps: Download File From SharePoint Document Library

Power Apps: Download File From SharePoint Document Library

With Power Apps you can download a file from a SharePoint document library to the user’s computer. Depending on the browser’s settings the file might save to the downloads folder only or it might open immediately in a new browser tab upon download. My preferred method uses a special trick to force Power Apps to save to downloads folder only. In this article I will show you how Power Apps can download a file from a SharePoint document library.

Table of Contents
• Introduction: The Vendor Invoices AppSetup The SharePoint Document LibraryCreate A List Of Vendor InvoicesShow The Filename And File Extension IconCreate A File Download ButtonDownload A Selected File From SharePointTest The File Download Button In Power Apps




Introduction: The Vendor Invoices App

Project managers at a construction company use the Vendor Invoices App to view a list of invoices and download the selected invoice. The invoices are stored in a SharePoint document library.




Setup The SharePoint Document Library

Create a new SharePoint document library named Vendor Invoices and add upload several documents to it. The documents may be of any file type (pdf, docx, xlsx, etc.).




Create A List Of Vendor Invoices

Open Power Apps Studio and create a new app from blank. Add a vertical gallery to the app.



Then connect the Vendor Invoices SharePoint document library to the app.



Use this code in the Items property of the gallery to display the Vendor Invoices files.

'Vendor Invoices'



Select the layout Image and Title to show a single image control and text control on each line.




Show The Filename And File Extension Icon

For each file in the gallery we want to display the filename and and icon.



Use this code in the Text property the label to show the filename and extension.

ThisItem.'File name with extension'



The image beside the filename will show a file extension icon.



Use this code in the Image property of the image. It is not necessary to load every file extension icon into the app. Instead, we can use the same static image assets as SharePoint. The code below extracts the file extension and then targets the proper SharePoint URL.

$"https://static2.sharepointonline.com/files/fabric/assets/item-types/24/{Last(Split(ThisItem.'File name with extension',".")).Value}.svg"




Create A File Download Button

We want the user to press a button next to the filename to download the file to their device. Select the existing Next Arrow icon in the gallery and change it to a download icon.



Use this code in the Icon property of the Icon.

Icon.Download



Then use this code in the icon’s Color and Fill properties to style it.


Color: Color.DarkBlue
Fill: Color.AliceBlue




Download A Selected File From SharePoint

The final step is to download the file to the user’s device when they press the download button. We only want it to download directly to the user’s device. And we want to prevent it from opening immediately.



Use this code in the OnSelect property of the download icon to download the file. Replace the SharePoint site path with your own SharePoint site.

Download($"https://matthewdevaney.sharepoint.com/sites/MatthewDevaneyBlog/_layouts/download.aspx?SourceUrl=https://matthewdevaney.sharepoint.com/sites/MatthewDevaneyBlog/{ThisItem.'Full Path'}")



As an example, we can get the SharePoint site path from a web browser while viewing the Vendor Invoices document library.




Test The File Download Button In Power Apps

We’re done. Press the download button to test downloading the file to your device.





Questions?

If you have any questions or feedback about Power Apps: Download File From SharePoint Document Library please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest

17 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Dinesh
Dinesh
8 months ago

Excellent share

Frank Nielsen
Frank Nielsen
8 months ago

Nice 🙂

Jerome
Jerome
8 months ago

Ah smart. _layouts/downloads.aspx. I am learning there are a lot of SharePoint _layouts to get things done. This is a gem. Curious though what is the $ for at the beginning of the fetch download url api?

JRay
JRay
8 months ago

Unfortunately, not all browsers will respect force download. It must be browser configuration. I’ve seen chrome and edge click on the same link and one tries to download while the other opens it 🙁

JRaymer
JRaymer
8 months ago

Very interesting – I wonder if it goes back to how the organization’s IT has set things up.

Mike
Mike
8 months ago

Thank you for this hint!
That was the missing piece of the puzzle to hide the URL of the datasource.

David
David
7 months ago

Hello Matthew,
Great article. Just to add an additional tip. Always encode the url in the fill path provided by SharePoint.

EncodeUrl(ThisItem.'Full Path')

Sherry
Sherry
3 months ago

What does EncodeUrl mean?
Download($”https://XYZ.sharepoint.com/sites/XYZ/_layouts/Download.aspx?SourceURL=https://XYZ.sharepoint.com/sites/XYZ/{ThisItem.’Full Path’}”)

I must be missing something. Mine gets an error saying File Not Found.

Carl Hansen
Carl Hansen
7 months ago

Perfect as always, thanks Matt.

Ganesh
Ganesh
4 months ago

Hey Matthew. Thanks for this article. I was able to download files when I access the Power Apps from web browser but when using a mobile or iPad device, it says “There was a problem downloading the specific content locally”. Is there any workaround for this?

Samuel
Samuel
3 months ago

Thanks for sharing. I am currently using a computer browser to test and it is normal. However, when testing on a mobile device, I still get an error message that cannot be downloaded. Is there a way to download normally on a mobile device?

Maciej
Maciej
2 months ago

I’m thinking how to load the exact image from Sharepoint list based od the condition: name of the image is present at other control.

Adith
Adith
26 days ago

How to download a specific version of a file ?