Table of Contents
When working with Canvas Apps in Power Apps, Dataverse and file columns, it’s very common to want to have a “download” button that lets the user save a copy of the file in their local computer.
This can be achieved in a way by using Dataverse API with the following endpoint:
{environment-url}/api/data/v9.0/table_logical_name({record-id})/column_logical_name/$value
However, this link will fail with 401 error in mobile and also if the user is not logged into Dynamics already – which is in almost all cases.
But don’t you worry! In this blog we will show you a workaround using Power Automate that will avoid all 401 errors that could happen.
Solution description
- We will create a Power Automate workflow with a “HTTP request” trigger (which will receive the record ID for which we want to download the file)
- We will use the Dataverse connector actions in Power Automate to download the file content
- We will return to the user the file content, and this will make the file to be automatically downloaded when user clicks the button in the canvas app
Step 1. Create Power Automate workflow
Start by creating a new flow and select the “instant cloud flow” type and select the “When an HTTP request is received” trigger.
Configure trigger as follows
Who can trigger the flow?: Anyone
Method: GET
Relative path: {guid}
Step 2. Extract file name
Use the “Get row by ID” action from the Dataverse connector and pass the “guid” variable in the flow that comes from the trigger as “Row ID”
Step 3. Extract file content
Add the “Download a file or an image” action from the Dataverse connection, select the relevant table and column and pass the “Guid” variable as the value for “row id”.
Step 4. Return file to user
Add the “Response” action as the final step for the workflow. We will rename to “Send file to user”
Set up the action as follows:
Status code: 200
Headers:
Content-Type application/octet-stream
Content-Disposition attachment;filename=”{file-name-from-table}”
Body: @{base64ToBinary(body(‘Download_file’)?[‘$content’])} (Power Fx expression to convert the Dataverse file from Base64 to Binary).
Step 5. Add Download function in Power Apps
This is the code you should add in the button or icon that will download the files from Dataverse in the app:
Download(HTTP_GET_URL_FROM_POWER_AUTOMATE)
Copy the URL from Power Automate and replace the “{guid}” part by the Record GUID from the Dataverse table that contains the attachment.
Download(Substitute(HTTP_GET_URL_FROM_POWER_AUTOMATE, “{guid}”,ThisItem.FileRecordID)
Conclusion
In short, Power GI’s method lets you add an easy download button in Canvas Apps that works smoothly on any device. Using Power Automate, this approach avoids 401 errors, so users can access files hassle-free, even if they aren’t logged into Dynamics.