Download large files via Dataverse

Introduction

Figure 1

Referring to Figure 1,

1.) On the Dynamics 365 form, a user selects a file to download (e.g. a word document).

2.) Within the form’s custom JavaScript, the custom API RetrieveFileFromExternalSystem() is called with the file id of the word document in the external system.

3.) In RetrieveFileFromExternalSystem(), the retrieved Base64 encoded file is placed in the RetrieveFileFromExternalSystem() JSON response message.

This solution works fine for files under 116.85 MB. For files over the size, the following error message is generated

Error Message: Message size exceeded when sending context to Sandbox. Message size: ### Mb. This error occurs when a message payload is greater than 116.85 MB, and a plugin-in is registered for the message. The error message includes the size of the payload that caused this error.

The next section presents a solution for downloading files which are over 116.85MB

Solution Overview

Figure 2

Referring to Figure 2,

1.) On the Dynamics 365 form, a user selects a file to download

2.) Within the form’s custom JavaScript code, the custom API RetrieveFileFromExternalSystem() is called. RetrieveFileFromExternalSystem() uses the file id of the external system to retrieve the file. If it’s a large file (over 116.85MB), the C# function UploadFile() is called (Figure 3)

3.) Within UploadFile(), a call is made to create a record in a custom dataverse table ‘File Staging’. Within this record the file is stored in a File column (cpl_temporaryfile)

4.) The record id of the ‘File Staging’ record is retrieved

5.) Within the form’s custom JavaScript, a call is made to downloadFile() to download the file (the file stored in the cpl_temporaryfile column of the ‘File Staging’ record) to the browser.

6.) After the file is downloaded, it’s deleted from the ‘File Staging’ record file (Figure 4)

Solution Detail

1.) To determine how to create the ‘File Staging’ table, please refer to part 1 of this post

2.) The UploadFile() C# custom API function (Figure 3)

Figure 3

3.) The downloadFile() TypeScript function (Figure 4). Note: The ‘Dataverse REST Builder’ tool (Figure 5) was used to generate the code

Figure 4

Appendix

Figure 5

Further reading

https://dustinminer.com/2024/04/11/upload-large-files-via-dataverse/