Using Xrm.Navigation.openFile to download a note attachment file with file save confirmation dialog

I got this requirement recently and wanted to share with my readers. Quite a cool feature actually and some common requirement. So there is a ribbon button on a entity form. When the ribbon button is clicked, it should query an annotation attachment, get the content and then throw the file save dialog option to the user to save or open the file.

Querying the annotation record is piece of cake after all. The interesting part is throwing the filesave dialog option.

Below is the code on click of the ribbon button which does exactly that.

function DownloadAnnotationRecord() {
    Xrm.WebApi.retrieveRecord(“annotation”, “<annotation_record_id>”, “?$select=filename,filesize,documentbody,mimetype”).
       then(function (r) {
          var fileOptions = {
             fileContent: r.documentbody,
             fileName: r.filename,
             mimeType: r.mimetype
          };

 

         var openOptions = { openMode: 2 };

 

         Xrm.Navigation.openFile(fileOptions, openOptions)
             .then(function () {
                console.log(“File downloaded successfully.”);
             }, function () {
                console.log(“File was not downloaded.”);
             });
       }, function (e) {

 

      });
}

Code is self explanatory. In chrome, the file will download. In edge, user shall get file save dialog option.

Hope this helps!

Debajit Dutta

(Business Solutions MVP)

For training & Consulting, please write to us at info@xrmforyou.com


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading