Wednesday 6 January 2016

Generate PDF File using java script in Microsoft Dynamics CRM 2015 online in just 1 click .

Hi All,
Today I Back with my New requirement to "Generate a PDF file of an open record in Just 1 Click "
The Image Below will give you the clear understanding what was real requirement was.

To solve this requirement we need to follow some steps :-

1. You need to create an SSRS Report for your Record by passing "RecordId" as parameter.
2. Get the ReportSession and ControlId for the called report.
3. Open the Report as a PDF Format.

After the SSRs Report has been Added in CRM Instance. You need to add a Button on Form Ribbon.

Once we done with SSRs Report and Button we will create a  java script file and code will be

Method to get the SessionID and ControlID for the SSRSReport getReportingSession: function () {
        var selectedIds = Xrm.Page.data.entity.getId();
        var reportName = "NameofReport.rdl";
        var reportGuid = getReportGuidByName("NameofReport");   //OR Report GUID - Replace with your report GUID

        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:parameterNamespecified in ssrs report=" + selectedIds);

        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;
    }
**Replace the Value which is specified with the Red Color in the code.

After we have received the Session Array we will call a method which will download the PDF file to our Local drive. Code is here.
Method to Download PDF. runReportToPrint: function () {       

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

        window.open(newPth, "_self");
    }
Hope this would help you in your custom development!!!
If you have any Query related the code!! Please leave the Comment!!!

28 comments:

  1. Can we do this with CRM Online as well? I am wondering how can I pass Report ID in online instance?

    ReplyDelete
    Replies
    1. Yes,you can.
      Please,can you explain me your requirement.

      if you want to make it dynamic just use the method "getReportingSession" with 2 parameter reportName and reportID and pass this 2 value and call it.

      Delete
  2. With which parameter name I can change this "&p:parameterNamespecified in ssrs report="?

    ReplyDelete
  3. It's an optional ,If you have created any parameter in ssrs report.
    E.g.:suppose you want to pass any filter condition to your report then you can use it or else it's optional.

    ReplyDelete
  4. i do all of that but the pdf is not download

    ReplyDelete
    Replies
    1. Use debugger to debug the code and find out the Error and share it here.
      OR
      If you are not login with "administrator" then You need to check the permission of the report to download it.

      Delete
  5. Hi, I have a requirement to put SSRS report in Mail body.. Can you help me out in this..

    ReplyDelete
  6. Hi Kamran,

    Please go through my this post
    http://crmpankaj.blogspot.in/2016/03/send-pdf-file-as-attachment-in-mail.html

    This will help you to get base64 content for your mail body.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. I am updating comment not sure why its getting deleted after sometime from blog..

    ReplyDelete
    Replies
    1. Hi Karman,
      I have seen your comment in mail.
      I am not sure but try to change the response type to "document" when doing XMLHttpRequest.

      Follow this link:-
      https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType

      Delete
  8. Hi Pankaj,

    How do we get the reportsession parameters for OOB report. Could you please help in framing the query?

    retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:parameterNamespecified in ssrs report=" + selectedIds);

    What would change in the above query?

    Thank you.

    Regards,
    Mohammed Hameed.

    ReplyDelete
    Replies
    1. Sorry for the late replay,
      You do not need to think differently for OOB report. just follow the same process and only you need to pass the OOB report name or its GUID when calling getreportsession method.

      Delete
    2. Pankaj - What would the retrieveEntityReq.send string look like when using an OOB report? I am getting a 404 - Not Found when trying to use this.

      Delete
  9. Hi

    I got it working without a parameter - however I don't know what the default parameter name if for the Entity ID (GUID) for example. I simply want to open the invoice report for the CRM invoice that the user is viewing - however if I open the report without any parameters it shows all invoices.

    Thanks again

    ReplyDelete
  10. Hi Tim,
    This parameter depends on the report, whether it has a parameter or not.
    if it has you can use that option. For you case create a report with a parameter as a currentID which would pass the current id of record which will open the invoice for current record.

    ReplyDelete
  11. Thanks Pankaj for the reply - the report I have is using the CRM Filtered views so it already had the context for the invoice. However when I don't pass the parameter the reporting prints all invoices. However when I run it normally from the CRM reports menu from the invoice it only prints the single invoice.

    Thanks again

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This is not working for OOB Quote Report in Dynamics 365. I get a HTTP 404-Not found error when I hit this code:

    retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:parameterNamespecified in ssrs report=" + selectedIds);

    ReplyDelete
  14. How do you retrieve the sessionid/controlid of an OOB report?

    ReplyDelete
    Replies
    1. Hi Phillip,

      It's the same way u can do it for OOB report.
      Hope it works for u.

      Delete
  15. Hi Phillip,
    Take care of "iscustomreport=false" and "parameterNamespecified" you need to open the OOB report and check what is the name of parameter and specify it accordingly in the url while calling from java script.

    ReplyDelete
  16. Hi Pankaj,
    I have the same requirement but, i m new to dynamics 365, can you please guide me where do we have to do all this coding? i am confused.

    ReplyDelete
  17. Hi Pooja
    It’s a JavaScript code and you can call this from ribbon button or as per your business requirements.

    ReplyDelete
  18. So, i just need to add a ribbon button and just add this code to the button? that's it?

    ReplyDelete
  19. Amazing Artice Written. I am very much glad to read your article.
    I am Following Your From Last 6 Month- Brother and really linking the stuff
    you post on your blog on Regular Basis.
    Keep Posting blogs like this….. Thanks alot

    WhatsApp API Integration

    ReplyDelete
  20. Hi Pankaj
    your are doing best work
    keep it up
    we provide WhatsApp API Integration With Tally

    ReplyDelete
  21. This is really a very good article. Thanks for taking the time to discuss with us, I feel happy about learning this topic. keep sharing your information regularly for my future reference. if you look out this our WhatsApp API Services. if any thing you need then please visit us https://umstechlabs.com/whatsapp-api-integration

    ReplyDelete