【原创】Dynamics CRM 2015/2016,以PDF的形式打开SSRS报表。

时间:2021-11-19 16:47:32

基本步骤:

  1. 使用SSRS建立报表,以下的例子是以记录的GUID作为报表的参数
  2. 获取ReportSession 和 ControlId来调用报表
  3. 以PDF的形式预览报表

一、根据报表的名称获取报表的GUID

//use sdk.soap java script library to fetch record.

  getReportGuidByName:function(reportName) {
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='report'>" +
"<attribute name='name' />" +
"<attribute name='reporttypecode' /> " +
"<attribute name='filename' /> " +
"<attribute name='reportid' />" +
"<filter type='and'>" +
"<condition attribute='name' operator='eq' value='" + reportName + "' />" +
"</filter>" +
"</entity>" +
"</fetch>"
var fetchQuery = new Sdk.Query.FetchExpression(fetchXml);
var results = Sdk.Sync.retrieveMultiple(fetchQuery);
return results.view().entities[0].attributes.reportid.fValue; }

二、取得报表的 SessionID 和 ControlID。

//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(reportName); //OR Report GUID - Replace with your report GUID
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; }

三、生成PDF报表并在新的窗口打开

//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");
}