“File” data type in CDS. All you may need to know about it.

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

Well this one is pure serendipity. I was working for a customer and while creating a new field for an entity, I could see a new data type called – “File”.

image

Honestly I haven’t heard of any announcement regarding this data type before and I was quite intrigued about what it is this data type. So I decided to dig deeper.

As I was trying from the classic solutions area, I decided to move on to the solutions area in PowerApps maker portal and check if that data type is showing up. And it indeed shows up.

image

As Microsoft is recommending, you should be using PowerApps maker portal more often than not. And the reason is there are some stuffs which you can only perform in maker portal and not is classic solutions area.

In the powerapps portal, you can set the size of the field. By default, the upload size is 32 MB. However you can set it to a max of 128 MB.

Few more basic info before I dive deep.

Is this available for all OOB and custom entities?

Yes.

Can we have more than one field of type “File”?

Yes.

Very well. And now comes the awesome news. When this field was introduced it was not available for Model driven apps. That was a big disappointment. But now the field can be included in Canvas apps, flows and model driven apps as well. Below is the screenshot of file data type in canvas app. And it works like a charm

image

I think, this completely changes the dynamics of file storage in CDS. Suddenly you have an ace to play with outside Notes and SharePoint integration.

I have written additional blogs about how to read and upload contents for File Attribute in Dynamics 365/ CDS using JavaScript. Refer them through below links.

Well well, this is nice. Bit how to download the content of these attributes? Coding is my forte, I always put some code samples wherever I can.

Detailed information is provided in docs – https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/file-attributes

In the docs link, there is sample code to query using REST API. Although .NET classes are mentioned, there is no sample code on how to download the file bytes using the same. This sample code is for the benefit of readers.

InitializeFileBlocksDownloadRequest initializeFile = new InitializeFileBlocksDownloadRequest(); 
            initializeFile.FileAttributeName = "cr269_filedatatype"; // attribute name 
            initializeFile.Target = new EntityReference("account", Guid.Parse("37F480BE-EA1F-EA11-A810-000D3A569DFF"));

            InitializeFileBlocksDownloadResponse initializeFileResponse = (InitializeFileBlocksDownloadResponse)service.Execute(initializeFile);

            Console.WriteLine($"File Name: {initializeFileResponse.FileName}"); 
             Console.WriteLine($"File size (bytes): {initializeFileResponse.FileSizeInBytes}");

            var fileContinuationToken = initializeFileResponse.FileContinuationToken;

            // code to downlod the file. 
            DownloadBlockRequest downloadRequest = new DownloadBlockRequest(); 
            downloadRequest.Offset = 0; 
            downloadRequest.BlockLength = (long)4 * 1024 * 1024; // can be max of 4 MB 
            downloadRequest.FileContinuationToken = fileContinuationToken;

            DownloadBlockResponse downloadBlockResponse = (DownloadBlockResponse)service.Execute(downloadRequest);

            byte[] fileBytes = downloadBlockResponse.Data;
Advertisements

Couple of important points with the above code. You can download a max of 16 MB in one call. For the remaining content you need to query again using the FileContinutationToken. The block size determines the maximum chunk that can be retrieved in further calls. The file bytes cannot be retrieved using RetrieveRequest or RetrieveMultipleRequest

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

6 thoughts on ““File” data type in CDS. All you may need to know about it.”

    1. Hi Phil, Thanks for reading my blog. Max file size is 128 MB. I wonder when it is introduced in Model driven apps, the dynamics of file storage is going to change completely once it is introduced.

      Cheers!
      Debajit

  1. Thanks Debajit, I read your blogs always. Since this cant be on the forms, whats its actual essence ?. I mean you can see it on the form but can be seen in flow, in what scenerio can it be used?

    1. Hi Victor,
      Thanks for reading my blog. As I have written it can be used in canvas apps and flows. It may be exposed in the future on the model driven apps as well. I haven’t heard of official announcement though. But we can expect it soon I guess

      -Debajit

Leave a Reply