Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics CRM 2015 On-Premise Generate CRM Report as PDF

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello everyone,

I am at a loss with a recent requirement that I have to download a custom report through the CRM and generate it as a PDF file so it can be sent elsewhere.

I have the ability to create a plugin, web service, or use javascript, but I don't have a direct SSRS report link to use and am told that I must grab the report from CRM in the way it is generated through CRM (Such as going to run Report > and picking the report).

Some of the things I've tried:

1. Added a button to the ribbon bar of the desired entity using the ribbon workbench.

2. Added custom JS to perform an ajax call to a custom webservice that is utilizing the CRM SDK.

3. Attempted to scrape the https://organizationurl/crmreports/viewer/viewer.aspx URL while also passing in the appropriate parameters such as id and record type.

4. Turn the parsed HTML into a PDF document.

This failed because I would only be returned a CRM login page instead of the html containing the actual report.

--------------------

I've also tried coping the following URL: https://community.dynamics.com/crm/f/117/t/205829

and using the example provided by Bharat.

However, with this example, among many others, my report session and controlid always return as some form of:

[L> <html> <head> , HTML> <html> <head> <scr]

Then, when I go to open the PDF file, I get a message from Adobe stating that the file is corrupt, I am assuming it is because I never get a good report session and controlid in the first place.

All of the examples that I can find, such as the one listed above, all mention CRM online. Can anyone confirm if they have seen an approach like this for on-premise, if so, would you mind sharing some insight on what made it work?

I am also open to any other suggestion such as how I may be able to access it with a plugin or webservice.

Thank you,

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2015 On-Premise Generate CRM Report as PDF

    Ravi,

    Thank you so much for this.

    I was struggling with this for a while and this was exactly what I needed.

    For anyone curious,

    since the JS I referenced above didn't work...

    I did the following:

    1. Add a button on the ribbon bar of an entity such as Quote.

    2. Perform an ajax call to a web service.

    3. Within the web service, follow the guide provided by Ravi to generate the PDF

    4. Do what ever you need to with the PDF such as attach it as a note or use it with the email entity

    5. Pass any information back to the ajax call successful function and perform any additional action.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Dynamics CRM 2015 On-Premise Generate CRM Report as PDF

    Hi,

    Did you try this:

    butenko.pro/.../ms-crm-2011-general-approaches-to-generation-of-reports

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2015 On-Premise Generate CRM Report as PDF

    To help provide a little bit more context.

    Right from Bharat's post....

    This is the code that I'm trying to use with a few tweaks (such as my report guid of course)

    var reportName = "quoteReport.rdl";  //Name of your invoice report

    var reportGuid = "16ACCA48-F2AC-E511-80FE-C4346BC576E8"; //GUID

    function runReportToPrint() {

    var invoicenumber = "Test";

    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=" + invoicenumber + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

    encodePdf(params);

    }

    function getReportingSession() {

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

    recordId = recordId.replace('{', '').replace('}', '');

    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' value='" + recordId + "' /> </filter></entity></fetch>";

    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:CRM_invoice=" + strParameterXML);

    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;

    }

    function encodePdf(responseSession) {

    var retrieveEntityReq = new XMLHttpRequest();

    var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";

    retrieveEntityReq.open("GET", pth, true);

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

    retrieveEntityReq.responseType = "arraybuffer";

    retrieveEntityReq.onreadystatechange = function () {

    if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {

    var binary = "";

    var bytes = new Uint8Array(this.response);

    for (var i = 0; i < bytes.byteLength; i++) {

    binary += String.fromCharCode(bytes[i]);

    }

    var bdy = btoa(binary);

    createNote(bdy);

    }

    };

    retrieveEntityReq.send();

    }

    function createNote(data) {

    var note = {};

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

    recordId = recordId.replace('{', '').replace('}', '');

    var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()

    invoicenumber = invoicenumber.substring(4, 9);

    var refInvoice = new Object();

    refInvoice.LogicalName = "invoice";

    refInvoice.Id = recordId;

    note.ObjectId = refInvoice;

    note.ObjectTypeCode = refInvoice.LogicalName;

    note.Subject = "Invoice: " + invoicenumber;

    note.MimeType = "application/pdf";

    note.DocumentBody = data;

    note.FileName = invoicenumber + ".pdf";

    XrmServiceToolkit.Rest.Create(

    note,

    "AnnotationSet",

    function (result) {

    //Alert user

    alert("Note Created");

    //Refresh data so user sees newly created note

    Xrm.Page.data.refresh(false);

    },

    function (error) {

    alert(error.message);

    },

    true

    );

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans