web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

JavaScript response XMLHttpRequest object not getting response

(0) ShareShare
ReportReport
Posted on by

I have a requirement to generate a SSRS fetch based report as a PDF and attach to Email 

-->I followed below blog 

   https://themscrmexpert.wordpress.com/2016/12/19/how-to-send-ssrs-report-as-a-pdf-in-email-in-dynamics-crm-online/

--> But response is not successful  where

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

--> Version 365 (9.X)

-->CODE

function sendInvoiceAsPDFInEmail() {
    debugger;
    //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.
    //runReportToPrint(arrReportSession);
    //EncodePdf(arrReportSession);

}

function executeReport() {

    // GUID of SSRS report in CRM.
    var reportGuid = "7C72B497-5DA9-E911-A83C-000D3A178672";

    //Name of the report. Note: .RDL needs to be specified.
    var reportName = "Bookinandbilling.rdl";

    // URL of the report server which will execute report and generate response.
    //var pth = Xrm.Page.context.getClientUrl()   "/CRMReports/rsviewer/QuirksReportViewer.aspx";
    var pth = Xrm.Page.context.getClientUrl()   "/CRMReports/rsviewer/ReportViewer.aspx";

    //This is the filter that is passed to pre-filtered report. It passes GUID of the record using Xrm.Page.data.entity.getId() 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 = "ABCF16E7-F944-EA11-A812-000D3A31EBEB";
    //var reportPrefilter = "";

    var reportPrefilter = "";
    //Prepare query to execute report.
    var query = "id={"   reportGuid  
        "}&uniquename="   Xrm.Page.context.getOrgUniqueName()
          "&iscustomreport=true&reportnameonsrs=&reportName="   reportName
          "&isScheduledReport=false&p:CRM_invoice="   reportPrefilter; //CRM_QuoteId   ":ID =ABCF16E7-F944-EA11-A812-000D3A31EBEB"


    //Prepare request object to execute the report.

    var retrieveEntityReq = new XMLHttpRequest();
    retrieveEntityReq.open("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.
    retrieveEntityReq.send(query);

    //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 params = arrResponseSession;
    var pth = Xrm.Page.context.getClientUrl()   "/Reserved.ReportViewerWebControl.axd?ReportSession="   params[0]   "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID="   params[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();
    retrieveEntityReq.open("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.
    retrieveEntityReq.send();
}

I have the same question (0)
  • Community Member Profile Picture
    on at

    In the below function

    function convertResponseToPDF(arrResponseSession) {

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

       var params = arrResponseSession;

       var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[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();

       retrieveEntityReq.open("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.

       retrieveEntityReq.send();

    }

  • Jeremy Xu Profile Picture
    on at

    Hi Sunil565,

    Are you able to share the error message?

    Jeremy

  • Community Member Profile Picture
    on at

    6153.Capture1.PNG

  • rexkenley Profile Picture
    110 on at

    Not really an answer to your question, but can I suggest using the fetch api?

    Example:

    https://github.com/rexkenley/CrmUG2019/blob/master/src/jsx/crmUG.jsx Lines 54 - 73

  • Community Member Profile Picture
    on at

    No. Not fulfills my requirement.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans