Dynamics CRM 2013 Custom Code creating a report as a PDF attachment


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



Comments (6)

  1. Nada Hammam says:

    I am still getting a corrupted pdf.

    Please Advice!

  2. BilNik says:

    In the  responseText of request, there is a commented line added that represents example of how the request will look like, and example od ReportSession and ControlID format. If you take these values with retrieveEntityReq.responseText.indexOf(…) this will always return values from the example provided in the request and not the correct values. ReportSession and ControlID repeat multiple times further in the request so, the correct values with least modifications can be retrieved with this code:

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

               var ret = new Array();

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

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

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

  3. Lin Lin says:

    Hi BilNik,

    It's working with your above script.

    Thanks for your script.

  4. Prasadh says:

    the above suggestion works thanks a lot.. i have one  more doubt can u say a way to convert binary to byte array and encode to 64 format in javascript ..since the BinaryToArray vbsscript works only for Internet Broser, i need to make it cross broswer compatible

    <script type="text/vbscript">

    Function BinaryToArray(Binary)

          Dim i

          ReDim byteArray(LenB(Binary))

          For i = 1 To LenB(Binary)

                 byteArray(i-1) = AscB(MidB(Binary, i, 1))

                 concat2Bdy(AscB(MidB(Binary, i, 1)))

          Next

          BinaryToArray = byteArray

    End Function

       </script>

  5. Santhosh says:

    this is not working in google chrome

    why??

  6. DFAB says:

    There is another solution to print pdf from crm, no custom html, no silverlight. everything in the assembly, and it support latest crm online.

    http://www.codeproject.com/…/Create-Generate-PDF-from-CRM-with-JScript-plugin

Skip to main content