web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to send SSRS Report directly to printer without report viewer preview in crm ? (solved)

(0) ShareShare
ReportReport
Posted on by 15

Hello,

I added a custom button to the ribbon to run a ssrs report with javascript

1768.Capture1.PNG

Is there any way to print  that report directly with this button without opening the report viewer

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Please refer to the link below to see how you can generate PDF

    jsfiddle.net/.../0816jdfq

    ankit.inkeysolutions.com/.../dynamics-crm-2011-creating-e-mail.html

    community.dynamics.com/.../205829

    And please refer to the link below to see how you might be able to print PDF from JS

    stackoverflow.com/.../print-pdf-directly-from-javascript

  • Arton Thaqi Profile Picture
    15 on at

    I managed to generate the file as pdf using this code but not to print it directly.

    Note: I needed to generate the pdf for my service/case ssrs report so i changed the reportName reportGuid (you can find both of them in your report url) and the CRM_invoice to CRM_incident parameter and inviocenumber to ticketnumber .

    var reportName = "Invoice.rdl";  //Name of your invoice report
    var reportGuid = "16ACCA48-F2AC-E511-80FE-C4346BC576E8"; //GUID of your invoice report


    function runReportToPrint() {

    var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()
    if (invoicenumber != null) {
    invoicenumber = invoicenumber.substring(4, 9);
    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";
    window.open(newPth, "_self");
    encodePdf(params);
    }
    else {
    alert("Invoice ID is Missing");
    }
    }

    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='invoice'><all-attributes /><filter type='and'><condition attribute='invoiceid' 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 
    ); 
    }

  • Arton Thaqi Profile Picture
    15 on at

    I managed to print it directly just made this change in code to the encodePdf function :

    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";
    var myWindow=window.open(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=Service+Order+Report&ContentDisposition=AlwaysInline&Format=PDF&rc:PrintOnOpen=true&rc:PageWidth=8.5in&rc:PageHeight=11in");

    myWindow.focus();
    myWindow.print();

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans