Advertisements
Home > CRM 2011, JScript, Ribbon > Opening a report using ribbon button in CRM 2011

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 🙂

Advertisements
  1. Inam
    June 14, 2013 at 9:15 PM

    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.

    • June 15, 2013 at 9:07 AM

      Hi Inam,

      Can you double check by keeping both URL’s side by side?. The report GUID comes with ‘{‘ & ‘}’ symbols, be cautious with that.

  2. belmondo1
    February 19, 2014 at 6:34 PM

    Hi, how pass multiple selected items please? I’m trying call report from ribbon on grid and need pass selected records.

  3. A.H
    March 24, 2015 at 4:31 PM

    Thanks 🙂
    that was very helpful !!

  4. rox
    August 21, 2015 at 12:42 AM

    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.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: