Restrict File Type in Attachment control in Canvas App

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

This one is pretty cool and it was delight to achieve this. The requirement here is pretty simple. Customer have Dynamics 365 instance and they have a canvas app with Dynamics 365 (CDS) as datasource. In CDS datasource, we have support for File Attribute where you can upload attachment for record.

Below is a contact form of CDS with the File attribute in action.

Attachment control in Canvas app.

The file attribute is displayed in “Attachment” control inside a canvas app. Now the requirement. We need to restrict the attachment type to only .pdf and .docx files. So let’s see how we can achieve this.

It’s actually quite easy. There is an event for attachment control – OnAddFile. To restrict file type, we need to attach a event handler for the OnAddFile event.

Check out my other posts

In the OnAddFile event, I write the below formula.

Restrict File Type in Attachment control - Formula

Text representation of the formula:

If(Not(EndsWith(First(Self.Attachments).Name, ".pdf")) And Not(EndsWith(First(Self.Attachments).Name, ".docx")),Notify("Only pdf and docx files are allowed", NotificationType.Error); Reset(Self))

Quick explanation of the code. I am checking if the file extension is neither .pdf or .docx. If neither is the extension, I am using the Notify function to show error message to the user.

Restrict file type in attachment control in canvas app.

And there you go! It’s like what I expected. Cool stuff.

Hope this helps!

Debajit Dutta (Business Solutions MVP)