Opening a report using ribbon button in CRM 2011
Hi,
To open the Report :-
- We need report GUID and report associated entity record GUID
- Get report GUID by using ‘Report Name’ using ‘retrieveMultiple’ function
- Prepare URl and open new window
Below are the functions
function openReport (reportName) {
var oDataSetName = “ReportSet”;
var columns = “ReportId”;
var filter = “Name eq ‘” + reportName + “‘”;
retrieveMultiple(oDataSetName, columns, filter, onSuccess);
currReportName = reportName;
}
var currReportName;
function onSuccess (data, textStatus, XmlHttpRequest) {
var serverUrl = Xrm.Page.context.getServerUrl();
if (data && data.length > 0) {
var serverUrl = Xrm.Page.context.getServerUrl();
var id = Xrm.Page.data.entity.getId();
var etc = Xrm.Page.context.getQueryStringParameters().etc;
var callReportId = id.replace(‘{‘, ”).replace(‘}’, ”);
var reportId = data[0].ReportId.replace(‘{‘, ”).replace(‘}’, ”);
var url = serverUrl + “/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=” + currReportName + “.rdl&id=%7b” + reportId + “%7d&records=%7b” + callReportId + “%7d&recordstype=” + etc;
window.open(url, “reportwindow”, “resizable=1,width=950,height=700”);
}
}
function retrieveMultiple(odataSetName, select, filter, successCallback) {
var serverUrl = Xrm.Page.context.getServerUrl();
var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
var odataUri = serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “?”;
if (select) {
odataUri += “$select=” + select + “&”;
}
if (filter) {
odataUri += “$filter=” + filter;
}
$.ajax({
type: “GET”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
url: odataUri,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
if (data && data.d && data.d.results) {
successCallback(data.d.results, textStatus, XmlHttpRequest);
}
else if (data && data.d) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
else {
successCallback(data, textStatus, XmlHttpRequest);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (XmlHttpRequest && XmlHttpRequest.responseText) {
alert(“Error while retrieval ; Error – ” + XmlHttpRequest.responseText);
}
}
});
}
- Create a new .Jscript file and copy above functions
- Replace double quotes & single quote symbols with keyboard symbols
- Add the .jscript as web resource
- Add “json” & “jquery” helper scripts as web resources
How Do I call these methods :-
- Assume you have a report named “My Account Report” associated (i.e., Related Record Types) to “Account” entity
- Place a ribbon button on account entity form
- Call openReport(“My Account Report”) function from ribbon button (How to call function from ribbon button)
Hope it helps 🙂
Thanks for sharing this.
I am facing issue, report URL is working fine in a separate IE window but not in the popup window that is opened by window.open method.
Hi Inam,
Can you double check by keeping both URL’s side by side?. The report GUID comes with ‘{‘ & ‘}’ symbols, be cautious with that.
Hi, how pass multiple selected items please? I’m trying call report from ribbon on grid and need pass selected records.
Thanks 🙂
that was very helpful !!
Hi,
Thanks for the article.
I’ve implemented the same but I am getting prompted for credentials to the report server whenever the button is clicked.
How do I get rid of prompt? Please let me know. Thanks.