Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

What to do in command of custom ribbon button to download a PDF on click?

(0) ShareShare
ReportReport
Posted on by 52

I'm trying to learn how to create a ribbon button that will allow a user to download a PDF I have uploaded as a web resource.

  1. Using the Ribbon Workbench I have created the button with a command that has no actions tied to it yet.
  2. I've also uploaded a PDF by doing a workaround and changing it's extension to HTML, however leaving the name with a .PDF extension.

I'm a bit unsure if now I need to create a JS web resource that makes a call to that web resource or can I create a simple command so that the user will have the PDF downloaded on their side after a click of the button.

I can't find any examples of this online, any help would be appreciated, thanks!

  • Suggested answer
    Summers Profile Picture
    Summers 52 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    Ended creating a record in a custom entity, and attaching the PDF file to that record as a note. Then with JS and using the known ID sent an API call to retrieve that record and manipulate it's annotation:

    function popUpShow() {
        $(top.document.body).find("#divPopupBuild").remove();
        var divContent = "
    " "
    " "" "
    "; $(top.document.body).append(divContent); } function popUpHide() { $(top.document.body).find("#divPopupBuild").remove(); } function retrievePDFs(RecordId) { popUpShow(); var req = new XMLHttpRequest(); debugger; req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v8.2/s_pdfs?$filter=s_name eq '" RecordId "'&$orderby=createdon desc", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); if (results.value.length > 0) { getAnnotation(results); } else { alert("No pdf is avalilabe."); popUpHide(); } } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } function getAnnotation(results) { var notesDownloadUrl = ""; var reqAnnotations = new XMLHttpRequest(); reqAnnotations.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v8.2/annotations?$filter=_objectid_value eq " results.value[0].s_pdfid, true); reqAnnotations.setRequestHeader("OData-MaxVersion", "4.0"); reqAnnotations.setRequestHeader("OData-Version", "4.0"); reqAnnotations.setRequestHeader("Accept", "application/json"); reqAnnotations.setRequestHeader("Content-Type", "application/json; charset=utf-8"); reqAnnotations.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); reqAnnotations.onreadystatechange = function () { if (this.readyState === 4) { reqAnnotations.onreadystatechange = null; if (this.status === 200) { var AnnotationResult = JSON.parse(this.response); if (AnnotationResult.value.length > 0) { var noteUrl = Xrm.Page.context.getClientUrl() "/notes/edit.aspx?id={" AnnotationResult.value[0].annotationid "}"; var notePage = null; var req = {}; req.type = "GET"; req.url = noteUrl; req.async = true; req.contentType = "application/json; charset=utf-8"; req.dataType = "html"; req.success = function (notePage, textStatus, XmlHttpRequest) { var contentType = XmlHttpRequest['Content-Type']; if (!!notePage) { var tage = 'span'; var regexp = new RegExp('] WRPCTokenUrl[^>] >'); var SpanTages = notePage.match(regexp); if (!!SpanTages) { var TokenTage = SpanTages[0] ''; var div = $('
    ').html(TokenTage); WRPCToken = $(tage, div).attr('WRPCTokenUrl'); if (!!WRPCToken) { var attachMentIds = AnnotationResult.value[0].annotationid; if (attachMentIds != null) { notesDownloadUrl = Xrm.Page.context.getClientUrl() "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId={" AnnotationResult.value[0].annotationid "}" WRPCToken; var b64Data = AnnotationResult.value[0].documentbody; var blob = b64toBlob(b64Data, 'application/pdf'); if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveBlob(blob, AnnotationResult.value[0].filename); } else { window.open(notesDownloadUrl); var blobUrl = URL.createObjectURL(blob); window.open(blobUrl); } popUpHide(); } } } } }; $.ajax(req); } else { popUpHide(); } } else { Xrm.Utility.alertDialog(this.statusText); } } }; reqAnnotations.send(); } function b64toBlob(b64Data, contentType, sliceSize) { contentType = contentType || ''; sliceSize = sliceSize || 512; var byteCharacters = atob(b64Data); var byteArrays = []; for (var offset = 0; offset < byteCharacters.length; offset = sliceSize) { var slice = byteCharacters.slice(offset, offset sliceSize); var byteNumbers = new Array(slice.length); for (var i = 0; i < slice.length; i ) { byteNumbers[i] = slice.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } var blob = new Blob(byteArrays, { type: contentType }); return blob; }

    Hope this helps anyone with a similar issue!

  • Suggested answer
    ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    This file approach will not workout or will be too complex with file extensions.

    I can suggest you some work arounds as follows:

    1. Common Cloud Folder (SharePoint / OneDrive - create shared folder / Google Drive - shared folder) - Use the link to open via button click

    1. User word template (embed your pdf document in word template and upload empty word template to D365. You can always download this from D365.

    2. Send an email with your PDF file as attachment to the current user of system.

    You can create an email template. Upload the attachment in the email template. On click of button update a boolean field on the form (Yes/no). On update of boolean field send an email to current user using the email template.

    These are just a work arounds.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,212 User Group Leader on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    Hello, 

    Please read this blog. You can find how to store your pdf file as a Web ressource

    us.hitachi-solutions.com/.../

  • Summers Profile Picture
    Summers 52 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    There is no SharePoint site we have that is common for everyone, so I would like to host it on Dynamics, the only way I know so far of how to upload files is by using the Web Resources.

  • ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    What is the use case here?

    If it is a static document you can store the file in common SharePoint folder and open on click of button(you can load sharepoint URL)

    Is it compulsory that file needs to stay in web resource?

  • Summers Profile Picture
    Summers 52 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    So I made a copy of the pdf file and added a .html extension, then uploaded that file as a web resource giving it the name Help.pdf, and making it type of Webpage (HTML), however it doesn't open up as a PDF in the way it originally use to before I renamed it.

  • Suggested answer
    ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    When naming this html Web Resource, you should still name the file with the normal .pdf extension

    sample web resource name: new_filename.pdf

    The file you upload might be html extension

    For javascript, user

    Xrm.Utility.openWebResource("new_filename.pdf");

  • Summers Profile Picture
    Summers 52 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    Hi there so it's not opening nor downloading the PDF as per what I'm trying to do, it's just trying to open the web resource.

  • ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    Hi Summers

    If it worked, Please close the thread by marking answer as verified.

  • Summers Profile Picture
    Summers 52 on at
    RE: What to do in command of custom ribbon button to download a PDF on click?

    Thanks for such a speedy reply!! So it works but I don't what to do because now it opens it up as a HTML file since I changed the extension of the PDF to HTML, so that I could upload it as web resource. Do you know how I can get around this? Thanks!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans