“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 wonder whether it will be ever exposed to the Model driven apps and if yes, I think it completely changes the dynamics of file storage in CDS. Suddenly you have an ace to play with outside Notes and SharePoint integration.

Well well, this is nice. But how about getting the data stored in these fields. As coding is my forte, I am always bit biased towards developers and here I am not let them high and dry.

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 the class for querying using .NET SDK is mentioned, there is no sample code on how to download the file bytes using the same. Hence I thought of putting this sample code 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;

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s