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
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.
If you have any Query related the code!! Please leave the Comment!!!
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!!!
Can we do this with CRM Online as well? I am wondering how can I pass Report ID in online instance?
ReplyDeleteYes,you can.
DeletePlease,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.
With which parameter name I can change this "&p:parameterNamespecified in ssrs report="?
ReplyDeleteIt's an optional ,If you have created any parameter in ssrs report.
ReplyDeleteE.g.:suppose you want to pass any filter condition to your report then you can use it or else it's optional.
i do all of that but the pdf is not download
ReplyDeleteUse debugger to debug the code and find out the Error and share it here.
DeleteOR
If you are not login with "administrator" then You need to check the permission of the report to download it.
Hi, I have a requirement to put SSRS report in Mail body.. Can you help me out in this..
ReplyDeleteHi Kamran,
ReplyDeletePlease 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.
This comment has been removed by the author.
DeleteI am updating comment not sure why its getting deleted after sometime from blog..
ReplyDeleteHi Karman,
DeleteI 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
Hi Pankaj,
ReplyDeleteHow 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.
Sorry for the late replay,
DeleteYou 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.
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.
DeleteHi
ReplyDeleteI 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
Hi Tim,
ReplyDeleteThis 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.
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.
ReplyDeleteThanks again
This comment has been removed by the author.
ReplyDeleteThis is not working for OOB Quote Report in Dynamics 365. I get a HTTP 404-Not found error when I hit this code:
ReplyDeleteretrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:parameterNamespecified in ssrs report=" + selectedIds);
How do you retrieve the sessionid/controlid of an OOB report?
ReplyDeleteHi Phillip,
DeleteIt's the same way u can do it for OOB report.
Hope it works for u.
Hi Phillip,
ReplyDeleteTake 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.
Hi Pankaj,
ReplyDeleteI 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.
Hi Pooja
ReplyDeleteIt’s a JavaScript code and you can call this from ribbon button or as per your business requirements.
So, i just need to add a ribbon button and just add this code to the button? that's it?
ReplyDeleteAmazing Artice Written. I am very much glad to read your article.
ReplyDeleteI 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
Hi Pankaj
ReplyDeleteyour are doing best work
keep it up
we provide WhatsApp API Integration With Tally
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