Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Empty SessionId and ControlId for Pdf Format of report when downloaded using Javascript

Posted on by

Hi All,

I am trying to download a report in pdf format using javascript. After the code is executed, the pdf is getting download but I am not able to open the pdf as it is showing format issue. 

I am using the below code to get sessionId and ControlId of the report and it is showing not found.

Please check and tell me if there is any error in the code below:

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);
return ret;

*This post is locked for comments

  • Suggested answer
    RE: Empty SessionId and ControlId for Pdf Format of report when downloaded using Javascript

    As per my knowledge reason behind this is request is not proper in any reason.

    I was experiencing the same problem when my RDL  file name was not correct , it was taking name "Invoice%20Report%20Detail"  in place of "Invoice Report Detail".

    I suggest use CRMREST Builder to get the name of RDL file name ,just pass GUID and get the name . and pass that same name in function  

  • Rakhi Sharma Profile Picture
    Rakhi Sharma on at
    RE: Empty SessionId and ControlId for Pdf Format of report when downloaded using Javascript

    Hi Ambar,

    I used the same code but it is showing the error of 404 not found for  sessionId and ControlId of the report.

    You have any idea why it is showing this error.

    Thanks

  • Suggested answer
    RE: Empty SessionId and ControlId for Pdf Format of report when downloaded using Javascript

    Hello ,

    Might be your request is not proper ,please try below code .just change rdl file name and pass crm parameter from java script function on ribbon button click .

    function runReportToPrint(selectedRecords) {

      try {

          debugger

          var selectedid = null;

          var reportid = null;

          var req = new XMLHttpRequest();

          var reportName = "Invoice Report Detail";

          req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/reports?$select=reportid&$filter=filename eq '" + reportName + ".rdl'", 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) {

                      debugger;

                      var results = JSON.parse(this.response);

                      if (results != null) {

                          reportid = results.value[0]["reportid"];

                          //if (selectedRecords != null && selectedRecords.length > 0) {

                          //    for (var i = 0; i < selectedRecords.length; i++) {

                          //        selectedid = selectedRecords[i];

                          //        var params = getReportingSessionhomegrid(reportName, reportid, selectedid);

                          //        var newPth = 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=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

                          //        setTimeout(function () { window.open(newPth, "_self"); }, 1000);

                          //    }

                          if (selectedRecords != null && selectedRecords.length == 1) {

                              selectedid = selectedRecords[0];

                              var params = getReportingSessionhomegrid(reportName, reportid, selectedid);

                              var newPth = 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=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

                              setTimeout(function () { window.open(newPth, "_self"); }, 1000);

                          }

                          else if (selectedRecords != null && selectedRecords.length == 0) {

                              alert("Please select a record.");

                          }

                          else if (selectedRecords != null && selectedRecords.length > 1) {

                              alert("Please select only one record .");

                          }

                          else {

                              var params = getReportingSession(reportName, reportid);

                              var newPth = 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=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

                              window.open(newPth, "_self");

                          }

                      } else {

                          Xrm.Utility.alertDialog(this.statusText);

                      }

                  }

              }

          };

          req.send();

      } catch (ex) {

          throw ex;

      }

    }

    function getReportingSession(reportName, reportGuid) {

      var selectedIds = Xrm.Page.data.entity.getId();

      selectedIds = selectedIds.replace('{', '');

      selectedIds = selectedIds.replace('}', '');

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

      var retrieveEntityReq = new XMLHttpRequest();

      retrieveEntityReq.open("POST", pth, false);

      retrieveEntityReq.setRequestHeader("Accept", "*/*");

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

      retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:invoiceid=" + selectedIds);

      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);

      return ret;

    }

    function getReportingSessionhomegrid(reportName, reportGuid, selectedrecordid) {

      // var selectedIds = Xrm.Page.data.entity.getId();

      selectedrecordid = selectedrecordid.replace('{', '');

      selectedrecordid = selectedrecordid.replace('}', '');

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

      var retrieveEntityReq = new XMLHttpRequest();

      retrieveEntityReq.open("POST", pth, false);

      retrieveEntityReq.setRequestHeader("Accept", "*/*");

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

      retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:invoiceid=" + selectedrecordid);

      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);

      return ret;

    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    function runReportToPrintReceipt() {

      try {

          debugger;

          var reportName;

          var chequeamount = Xrm.Page.getAttribute("cm_chequeamount").getValue();

          if (chequeamount != null) {

              reportName = "Receipt_Cheque";

          }

          else {

              reportName = "Receipt_Cash";

          }

          var selectedid = null;

          var reportid = null;

          var req = new XMLHttpRequest();

          req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/reports()?$select=reportid&$filter=filename eq '" + reportName + ".rdl'", 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) {

                      debugger;

                      var results = JSON.parse(this.response);

                      if (results != null) {

                          //reportid = "EEE6E34A-5F1B-E711-8102-5065F38B06F1";

                          var reportid = results.value[0]["reportid"];

                          var params = getReportingSessionReceipt(reportName, reportid);

                          var newPth = 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=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

                          window.open(newPth, "_self");

                      }

                  } else {

                      Xrm.Utility.alertDialog(this.statusText);

                  }

              }

          };

          req.send();

      } catch (ex) {

          throw ex;

      }

    }

    function getReportingSessionReceipt(reportName, reportGuid) {

      var selectedIds = Xrm.Page.data.entity.getId();

      selectedIds = selectedIds.replace('{', '');

      selectedIds = selectedIds.replace('}', '');

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

      var retrieveEntityReq = new XMLHttpRequest();

      retrieveEntityReq.open("POST", pth, false);

      retrieveEntityReq.setRequestHeader("Accept", "*/*");

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

      retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:ReceiptID=" + selectedIds);

      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);

      return ret;

    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans