1. 程式人生 > >【原創】Dynamics CRM 2015/2016,以PDF的形式開啟SSRS報表。

【原創】Dynamics CRM 2015/2016,以PDF的形式開啟SSRS報表。

基本步驟:

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