Today I will show how to "
Generate a PDF file of an open record in just one click." The image below will give you a clear understanding about what we are going to discuss.
The idea to solve this problem was simple: First develop a SSRS Report and save it as PDF. Here are the steps required to follow to solve this requirement:
- You need to create an SSRS Report for your Record by passing "RecordId" as parameter.
- Get the ReportSession and ControlId for the called report.
- Open the Report as a PDF Format.
After the SSRS Report has been Added in CRM Instance, you need to add a Button on Form Ribbon. Once we are done with SSRS Report and Button we will create a Java script file and the code will be,
Method to get the SessionID and ControlID for the SSRSReport
- getReportingSession: function()
- {
- var selectedIds = Xrm.Page.data.entity.getId();
- var reportName = "NameofReport.rdl";
- var reportGuid = getReportGuidByName("NameofReport");
-
- 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:parameterNamespecified in ssrs report=" + 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;
- }
Replace the Value which is specified with the Red Color in the code.
After we have received the Session Array we will call a method which will download the PDF file to our local drive.
Code is here.
Method to Download PDF
- runReportToPrint: function()
- {
-
- 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";
-
- window.open(newPth, "_self");
- }
Hope this would help you in your custom development. If you have any Query related to the code please mention in comments section below.