Customers have been using the below blog article to create custom code where a button is added to an entity form and when clicked on by a user, generates the appropriate report and saves as attachment in Notes section in the entity.

http://xrmmatrix.blogspot.cz/2011/06/creating-report-as-pdf-attachment-in.html?showComment=1402040197279 

We understood this worked fine in CRM 2013 until SP1/ Spring Update for CRM 2013 was released and now when the user tries to open the generated PDF report, it would state the file was corrupt.

The responseBody is undefined and this code is attaching a corrupt PDF. The problem is coming from the ReportSession Received is always the Same ReportSession=repl51vgqoms4g3fjynorp45, for example.

The following function had issue in the code :-

function getReportingSession() {

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

            var retrieveEntityReq = new XMLHttpRequest();

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

            var quotationGUID = Id.replace('{', ""); //set this to selected quotation GUID

            quotationGUID = quotationGUID.replace('}', "");

            var reportName = "Quote"; //set this to the report you are trying to download

            var reportID = "6A39D18F-2EC6-E344-8986-F49D6765A723"; //set this to the guid of the report you are trying to download

            var rptPathString = ""; //set this to the CRMF_Filtered parameter

            var strParameterXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='quote'><all-attributes /><filter type='and'><condition attribute='quoteid' operator='eq' uitype='quote' value='" + quotationGUID + "' /> </filter></entity></fetch>";

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

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

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

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

                            reportName + "&isScheduledReport=false&p:CRMAF_Filteredquote=" + strParameterXML;

            //remove the part starting from &p:salesorderid if your report has no parameters

            retrieveEntityReq.send(rptPathString);

            var x = retrieveEntityReq.responseText.indexOf("ReportSession=");

            var ret = new Array();

            ret[0] = retrieveEntityReq.responseText.substr(x + 14, retrieveEntityReq.responseText.indexOf("&", x) - x - 14); //the session id

            x = retrieveEntityReq.responseText.indexOf("ControlID=");

            ret[1] = retrieveEntityReq.responseText.substr(x + 10, retrieveEntityReq.responseText.indexOf("&", x) - x - 10); //the control id

            return ret;

        }

 

In the latest release some comments were added in the HTML Response. In those comment there is a sample URL exactly similar to the one searching in the HTML Response.

To fix this custom code to generate PDF reports, you have to skip a part of the Response first.

You should correct it by adding the following code in:

var StartAfterComment = retrieveEntityReq.responseText.indexOf("e440105867d249ce8a45696677d42bcb") + 32;

           var x = retrieveEntityReq.responseText.indexOf("ReportSession=", StartAfterComment);

            var ret = new Array();

            ret[0] = retrieveEntityReq.responseText.substr(x + 14, retrieveEntityReq.responseText.indexOf("&", x) - x - 14); //the session id

            x = retrieveEntityReq.responseText.indexOf("ControlID=", StartAfterComment);

            ret[1] = retrieveEntityReq.responseText.substr(x + 10, retrieveEntityReq.responseText.indexOf("&", x) - x - 10); //the control id

 

Note: This custom code solution is not created or supported by Microsoft Dynamics CRM Support.

 

Best Regards

Dynamics CRM Support Team

Share this Blog Article on Twitter

Follow Us on Twitter