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

9 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Dinesh
Dinesh
28 days ago

Excellent share

Frank Nielsen
Frank Nielsen
27 days ago

Nice 🙂

Jerome
Jerome
27 days 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
26 days 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
18 days ago

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

Mike
Mike
25 days ago

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