Power Apps: Download File From Dataverse File Column
Power Apps: Download File From Dataverse File Column Posted by - Matthew Devaney on - April 21, 2024 51 Comments You can create a button in Power Apps to dow...
- Published
- Reading time
- 4 min read
Before you start
Is this guide for you?
- Best entry point
- Dataverse
- Time investment
- 4 min read
Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/power-apps-download-file-from-dataverse-file-column/. Author: Matthew Devaney.
Power Apps: Download File From Dataverse File Column Posted by - Matthew Devaney on - April 21, 2024 51 Comments
You can create a button in Power Apps to download a file stored in a Dataverse file column. To do this, you must use the Dataverse Web API . If you haven’t used the Web API before it might be a little bit confusing . In this article I will show you how to use the Dataverse Web API in Power Apps to download a file from a file column. Table of Contents Introduction: The Download File Button
Create The Dataverse Table With A File Column Add A Download Button To A Canvas App
Construct A Dataverse Web API Request To Get A File
Store The Environment URL In An Environment Variable
Reference The Environment Variable In A Canvas App Introduction: The Download File Button
A Power Platform developer is making an app to give Project Managers access to Vendor Invoices. When a Project Manager selects the Download File button the file is downloaded from Dataverse to their device.
Create The Dataverse Table With A File Column
Setup a new Dataverse table named Vendor Invoices . Include the following columns and file types. Name (text) Invoice File (file type)
Add a new record to the table and store a PDF file in the Invoice File column.
This example uses a sample invoice from the AI Builder Lab but we can use any valid PDF. Add A Download Button To A Canvas App
When a user selects the Download File button in a canvas app the file is downloaded to their device. To do this, we must construct a URL to make a request to GET the file using the Dataverse Web API . The example below uses a URL built to get the PDF file we uploaded earlier. I will show you the required structure of the URL first and then show you how to get the pieces to build your own.
Open Power Apps Studio and create a new app from blank. Insert a button onto the screen.
Use this code in the OnSelect property of the button to download the file . The URL retrieves a table row using the Web API for the Vendor Invoice record with the unique identifier 23b9bbbe-0bee-ee11-a1fd-000d3af40608 and gets the Invoice File. I will show you how to build your own URL in the next section of this tutorial.
Download($ "https://org81a4e3c2.crm3.dynamics.com/api/data/v9.2/md_vendorinvoices(23b9bbbe-0bee-ee11-a1fd-000d3af40608)/md_invoicefile/$value" ) Code language: JavaScript ( javascript )
Test the button in Power Apps to ensure it works.
Construct A Dataverse Web API Request To Get A File
To build your own URL you will need to use this structure and supply your own environment url , table name , record id and column name . We will get this information by using the Dataverse Web API.
https:// < environment_url > /api/data/v9.2/ < table_name > ( < record_id > )/ < column_name > /$value Code language: HTML, XML ( xml )
Get the environment url from the the Power Platform admin center.
Find the table name by navigating to https://<environment url>/api/data/v9.2 in your web browser and inspecting the JSON output.
Then use the Web API to get a list of all the invoice file records using the URL https://<environment_url>/api/data/v9.2/<table_name> . Then get the record id of the record the file is found within.
Finally, get the column name using the URL https://<environment_url>/api/data/v9.2/<table_name>(<record_id>) .
Store The Environment URL In An Environment Variable
Hardcoding the environment URL is not a good idea because it will remain the same when moving the app from the development environment to the production environment. We should reference an environment variable which holds the environment URL in our code instead.
Create a new environment variable inside of a solution.
Make the display name Environment URL. Choose the data type Text. Then write the environment url for in the Current Value field.
Reference The Environment Variable In A Canvas App
To use the new environment variable in the canvas app connect it to the following two data tables: Environment Variable Defintions Environment Variable Values
Then replace the OnSelect property code of the download file button with this code instead. It performs a lookup on the environment variable values table and gets the environment variable with the matching display name.
Download( $ "{LookUp( 'Environment Variable Values', 'Environment Variable Definition'.'Display Name' = " Environment URL ", Value )}/api/data/v9.2/md_vendorinvoices(23b9bbbe-0bee-ee11-a1fd-000d3af40608)/md_invoicefile/$value" ) Code language: PHP ( php )
The download file button still works exactly the same as before but now the app can be moved to a different environment and point to a different database. Did You Enjoy This Article? 😺
Subscribe to get new Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up Questions?
If you have any questions or feedback about Power Apps: Download File From Dataverse File Column 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 M365 Copilot Power Apps
How To Create Copilot Custom UI Widgets In Power Apps
Tagged
Dataverse
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related comparisons
Next action