Download large files via Dataverse

This post describes how to leverage Dataverse in order to download large files from a server to a Dynamics 365 form

Note: the solution described below leverages the Dataverse File column to store the large file

Problem Description

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.) Within the RetrieveFileFromExternalSystem class, 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 (Figures 5 & 6) was used to generate the code

Figure 4

Appendix

Figure 5
Figure 6

Custom API

The custom API RetrieveFileToExternalSystem is defined as follows

There are two input parameters

  • Document Request
  • Document Request Type

The ‘Document Request’ contains the following values (which are passed to the custom API as a JSON)

  • relatedEntityName
  • relatedEntityId
  • files
    • name
    • extension
    • size
    • sizeInBytes
    • mimeType
    • lastModifiedDate
    • securityClassification
    • dcpGenerated
    • fileStagingId
    • documentGenerationMethod
    • content

The Document Request Type can contain the following values

  • RetrieveDocument

Further reading

Upload large files via Dataverse

References

https://learn.microsoft.com/en-us/power-apps/maker/data-platform/types-of-fields