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 CRM (Archived)

Converting rdl to pdf on button Click

(0) ShareShare
ReportReport
Posted on by 195

Hi friends,

I am trying to convert oob report to pdf on button click.

This is the code from our dynamics community threads only, report is not downloading. can anyone please help me to solve this issue.

Thanks,

Bharath

==============================

function runReportToPrint() {
debugger;
var params = getReportingSession();

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=public&ContentDisposition=OnlyHtmlInline&Format=PDF";
encodePdf(params);
window.open(newPth, "_self");
}

function getReportingSession() {

// var selectedIds = Xrm.Page.data.entity.getId();
var reportName = "Account Overview";
alert(reportName);
var reportGuid = "2B2A692E-7D04-E711-80E6-00155DB8652A";
alert(reportGuid);//OR Report GUID - Replace with your report GUID


var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/ReportViewer.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);

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 encodePdf(responseSession) {

var retrieveEntityReq = new XMLHttpRequest();
var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
retrieveEntityReq.open("GET", pth, true);
retrieveEntityReq.setRequestHeader("Accept", "*/*");
retrieveEntityReq.responseType = "arraybuffer";
retrieveEntityReq.onreadystatechange = 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]);
}
var bdy = btoa(binary);

alert(bdy);

}
};

retrieveEntityReq.send();
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shidin Haridas Profile Picture
    3,499 on at

    Hi Bharat,

    Have gone down this route to get the pdf version to be auto-downloaded.

    Pretty sure that the following lines of code are not returning the right access keys, which is required by the reporting services to generate the report.

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

    Since you seem to be on the online versio, why dont you try using Ms-flow to an extent to avoid the amount of code. Again, depends on what the report is doing and how complex it is.

    If my answer helps you, please mark it as verified to help the next person landing on this post.

  • bharrath Profile Picture
    195 on at

    Hi Haridas,

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

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

    these lines are not returning right access keys,.

    below line getting error

    "id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName

    Thanks,

    Bharath

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans