How to send SSRS report as a PDF in Email in Dynamics CRM Online


In this blog, I have explained how to create an email record in CRM by executing SSRS report and exporting as a PDF attachment to the email. This is frequently asked requirement by many clients and it is very simple to execute SSRS report and export as a PDF in CRM Online.

In this blog, I have kept focus on generating PDF format report using coding. I have also provided the code of generating PDF format report. I have not covered creating email record with attachment and reference links from this blog can be used to achieve the same.

Problem Statement/Assumptions

  1. We have a CRM Online environment with below things setup.
    1. Fetch based SSRS report with name let’s say ‘Quotation.rdl’.
    2. Assume, Quotation.rdl has below GUID.
      • 12f3c105-fd95-44a9-b1af-ad79dfe3ad16
    3. Quotation report is pre-filtered by Quote GUID. (What is pre-filtering?)
    4. On Quotation entity, we have a button called Send Quotation.
      • The purpose of this button is to execute SSRS report, Export it as a PDF, Create Email record with above generated PDF as an attachment & open Email record on successful completion of above operations.

Data/Events flow


Step-by-step procedure

  1. Create an action in CRM on Quote Entity.
    1. Input Parameter: PDFString (String type)
    2. Output Parameter: EmailID (EntityReference to Email entity)
    3. Create email record in action definition.
  2. Write a plugin on above custom action that will read input parameter which is a string that contains base64 formatted SSRS report. Create an attachment using the string and attach the same to email.

To know more about actions in CRM:

  1. Create a web resource with below JavaScript code (Code has the commenting to understand the code).

function sendQuoteAsPDFInEmail() {

    //1. Execution starts here.

    var arrReportSession = executeReport(); //2. Execute the ssrs report and capture the response. Response will be an array.

    convertResponseToPDF(arrReportSession); //3. Convert the response in base 64 string i.e. PDF.


function executeReport() {

    // GUID of SSRS report in CRM.

    var reportGuid = “12f3c105-fd95-44a9-b1af-ad79dfe3ad16”;

    //Name of the report. Note: .RDL needs to be specified.

    var reportName = “Quotation.rdl”;

    // URL of the report server which will execute report and generate response.

    var pth = Xrm.Page.context.getClientUrl() + “/CRMReports/rsviewer/QuirksReportViewer.aspx”;

    //This is the filter that is passed to pre-filtered report. It passes GUID of the record using method.

    //This filter shows example for quote report. If you want to pass ID of any other entity, you will need to specify respective entity name.

    var reportPrefilter = ”                      “;

    //Prepare query to execute report.

    var query = “id=%7B” + reportGuid +

        “%7D&uniquename=” + Xrm.Page.context.getOrgUniqueName()

        + “&iscustomreport=true&reportnameonsrs=&reportName=” + reportName

        + “&isScheduledReport=false&p:CRM_QuoteId=” + reportPrefilter;

    //Prepare request object to execute the report.

    var retrieveEntityReq = new XMLHttpRequest();“POST”, pth, false);

    retrieveEntityReq.setRequestHeader(“Accept”, “*/*”);

    retrieveEntityReq.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);

    //This statement runs the query and executes the report synchronously.


    //These variables captures the response and returns the response in an array.

    var x = retrieveEntityReq.responseText.lastIndexOf(“ReportSession=”);

    var y = retrieveEntityReq.responseText.lastIndexOf(“ControlID=”);

    var ret = new Array();

    ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24);

    ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32);

    //Returns the response as an Array.

    return ret;


function convertResponseToPDF(arrResponseSession) {

    //Create query string that will be passed to Report Server to generate PDF version of report response.

    var pth = Xrm.Page.context.getClientUrl() + “/Reserved.ReportViewerWebControl.axd?ReportSession=” + arrResponseSession[0] + “&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=” + arrResponseSession[1] + “&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF”;

    //Create request object that will be called to convert the response in PDF base 64 string.

    var retrieveEntityReq = new XMLHttpRequest();“GET”, pth, true);

    retrieveEntityReq.setRequestHeader(“Accept”, “*/*”);

    retrieveEntityReq.responseType = “arraybuffer”;

    retrieveEntityReq.onreadystatechange = function () { // This is the callback function.

        if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {

            var binary = “”;

            var bytes = new Uint8Array(this.response);

            for (var i = 0; i < bytes.byteLength; i++) {

                binary += String.fromCharCode(bytes[i]);


            //This is the base 64 PDF formatted string and is ready to pass to the action as an input parameter.

            var base64PDFString = btoa(binary);

            //4. Call Action and pass base 64 string as an input parameter. That’s it.



    //This statement sends the request for execution asynchronously. Callback function will be called on completion of the request.



  1. Register a JavaScript action on button that calls ‘sendQuoteAsPDFInEmail’ method.
  2. That’s it.


  1. Create Email and Attachment in CRM using C#

  1. More about Actions in CRM

  1. Snapshot of code to generate Email record with PDF from base64 string.

 generate email with pdf attachment code snapshot


11 thoughts on “How to send SSRS report as a PDF in Email in Dynamics CRM Online

  1. Thanks Yadnyesh for Sharing a Blog which is often requirement of my Client and you have architect a great solution with in-depth explanation in the blog.

  2. Hi Yadnyesh, many thanks for sharing this. Would it be possible to share the source code of the plugin that would convert the base64 string to PDF attachment?

    • Hi Santosh,

      Thanks for reading my blog post. I won’t be able to provide entire source code of my plugin because it contains lot of business logic, but you can refer the references section of this blog post. I have just updated it with snapshot for creating an email with attachment from base64 string. You can use that code as a reference in your plugin.

      Thank you once again for reading my blog.

  3. I am having issues getting this to work. Every time I hit the query variable I received a 404-Not Found error. Any ideas why?

  4. Hi Yadnyesh,

    Thanks for sharing. Do you know any way to render a report to pdf in C# inside a plugin or custom workflow activity for Dynamics 365 online? So far I couldn’t find any references to a C# solution how this can be done (if possible). Any suggestions welcome.


  5. Hi, i hve followed your blog and got to the part where the pdf is generated and attached to an email however the attached pdf generated cannot be opened when used in D365 online. I keep getting an error failed to load pdf document. Any ideas on what this could be?

      • Hi, I actually just figured out what the issue was finally! For anyone who cannot get it to work in CRM v9 please note that some developer left a comment in the response received from the report viewer where the sessionId and the controlId are dummy IDs. The actual session and control Ids are further down. When calling indexOf ReportSession it will return the dummy Id which in turn results in an error when actually rendering the report and as a repercussion generating an invalid PDF document. Please see the comment below:

        //CRM 261784: When exporting a report, the ReportViewer calls with a url to download.
        //In Outlook, we’re in an Outlook Inspector window, and the new window opens a Web Browser. This creates a new session,
        // forcing the user to sign in again, and the load balancer may pick a different Server, which causes the download to fail because of the new session.
        //Currently the only call to on this page is from the ReportViewer export button. So overriding it to make sure we open URL within the same window.
        // download URL from the ReportViewer:
        // “/Reserved.ReportViewerWebControl.axd?ReportSession=repl51vgqoms4g3fjynorp45&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=e440105867d249ce8a45696677d42bcb&OpType=Export&FileName=Report1&ContentDisposition=OnlyHtmlInline&Format=XML”

        To get it to work i changed the following:

        var x = retrieveEntityReq.responseText.lastIndexOf(“ReportSession=”);
        var y = retrieveEntityReq.responseText.lastIndexOf(“ControlID=”);

Leave a Reply

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

You are commenting using your 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