Sunday 1 April 2012

Expport FetchXml reports as PDF Attachment in CRM 2011 online

Generating reports in CRM 2011 is cool. Exporting a report as PDF is great. Attaching the PDF version of a report to a record or an email is a lot of clicks. Automating the process is actually quite easy, for an On Premise installation that is. With on premise, you have access to your report server and you can execute .Net code in any way you want. So hooking up the Reporting Services web services and CRM Web services in a single program makes it very easy to stream a report as a PDF and attach it to a CRM record. But when you try and do that in CRM online, you hit a couple of issues: You do not have access to Microsoft's report servers, nor can you run any .Net code wherever you want (unless you have a spare Windows Azure account). So how do we attach a report to a CRM record? You have to perform the following tasks:

1. Initiate a report session to get some session id's
2. Use these id's to request a report in pdf format
3. Convert the binary output to a base64 encoded string
4. Create a new annotation, and "attach" the encoded pdf file.
5. Do it all from javascript so that you don't have authentication issues, and you can upload it as a web resource.

It took me a couple of hours to get through these tasks, mainly because Internet Explorer is stubborn.
Since CRM 2011 only supports Internet Explorer, we have to use it, but on the bright side, whatever tricks we use to make it work do not have to be ported to any other browsers.

So using the XMLHttpRequest object, we can issue a request to the CRM Reportviewer control, and it will return a session and control id as part of the response. Next we use the XMLHttpRequest object again to request a PDF version of the report. The problem here is that Internet Explorer returns the binary stream in the XMLHttpRequest object's resonseBody property, but for some odd reason it is not accessible to JavaScript. VBScript on the other hand can access it with no problem. So I have a really ugly piece of VBScript code that loops through the contents, and calls a JavaScript function that concatenates the contents into an array. Yuk! But it works. Next we encrypt the stream into Base64, but thanks to my favourite search engine, it was easy to find a javascript function that helps us out. Once it is in Base64, we can create a CRM Annotation object, and using JSON, send it to the CRM REST endpoint to create it in CRM.

So the code below is an HTML web resource that you can modify and attach to any entity. It has a button that, when clicked, will generate a pre-defined report and attach it to the current entity as a note attachment. Basically the only parts that you need to modify are the report name and guid. You can find your report's guid by opening the report record and looking at the URL, and the name is the name that you gave it in CRM. My report is called "Public". Even though this code just creates an attachment, it is a simple excersise to create an email record with the report as an attachment.
Please see for more details CRM Reports as PDF


  1. Hello!

    I'm new to CRM.I'm trying to add a Custom Report as PDF attachment.
    I saw this post and tried the same way.But i have an issue regarding this.When i tr to attach the Report, the attachment contains all the record's reports.How can i have only one attachment relating to a particular record attached?
    Any suggestions would be appreciated!


  2. Hello,

    Do it also work if you have a sub-report ? (like for invoices, you got invoice lines)