How to load SSRS report in Web resources In CRM Online

If we have a requirement to show the report on any web resource in dynamics CRM online.

We can utilize the URL to load the report in Web resource IFrame.

below is the code for getting the URL for the report.

 

var reportName = “YourReportNameHere”, parentEntity = window.parent.Xrm.Page.data.entity  // Incase your report runs on speficic record.

url = serverUrl + “/crmreports/viewer/viewer.aspx?action=run&helpID=” + reportName + “.rdl&id={“;

//We would need to get the guid of the report and record id in case this report run for a specific record passing parameter.
url = url + GetTheGuidForReport(reportName + “.rdl”) + “}” + “&p:recordid=” + parentEntity.getId();
url = encodeURI(url);

//Finally load the URL In IFrame.

$(“#iFrameForReports”).attr(‘src’, url).attr(‘scrolling’, ‘yes’).attr(‘position’, ‘relative’).attr(‘frameBorder’, ‘0’);

 

 

//This function can be used for loading the report  Guild by just passing the report name.
function GetTheGuidForReport(reportName) {
var odataSelect = “$select=filename,name,reportid,reportidunique&$filter=filename eq ‘” + reportName + “‘”,
requestResults = retrieveMultipleRecordsSync (“reports”, odataSelect);
if (requestResults !== null) {
if (requestResults.length > 0) {
if (requestResults[0].reportid === null) {
return false;
} else {
return requestResults[0].reportid;
}
}
}
};

 

 

////************below code is only for reference***************//

//you might want to write your own code for retrieving, as below code is more of generic and here only for reference.

retrieveMultipleRecordsSync = function (type, options, errorCallback) {
stringParameterCheck(type, “retrieveMultipleRecordsSync requires the type parameter is a string.”);
if (options !== undefined && options !== null) {
//stringParameterCheck(options, “retrieveMultipleRecordsSync requires the options parameter is a string.”);
}

var optionsString, req, results;
if (options !== null) {
if (options.charAt(0) !== “?”) {
optionsString = “?” + options;
} else {
optionsString = options;
}
}

req = new XMLHttpRequest();
req.open(“GET”, getWebAPIPath() + type + optionsString, false);
setRequestHeaders(req); // this function sets the headers for the request.

req.onreadystatechange = function () {
/* complete */
if (this.readyState === 4) {
req.onreadystatechange = null;
switch (this.status) {
case 200:
var queryOptions,
returned = JSON.parse(this.responseText, dateReviver);
results = returned.value;
break;
default:
if (errorCallback !== undefined && errorCallback !== null) {
///errorCallback(errorHandler(this));  // handle the error in your own code.
}
break;
}
}
};
req.send();

return results;
};

//to set the headers of the request.

setRequestHeaders = function (req, includeFormattedValues, customHeaders) {
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
if (!(includeFormattedValues !== undefined && includeFormattedValues !== null && includeFormattedValues === false)) {
req.setRequestHeader(“Prefer”, “odata.include-annotations=\”OData.Community.Display.V1.FormattedValue\””);
}

if (!(customHeaders && customHeaders.constructor === [].constructor)) {
customHeaders = [];
}
customHeaders.forEach(function (h) {
switch (h.key) {
case “Accept”:
case “Content-Type”:
case “OData-MaxVersion”:
case “OData-Version”:
case “Prefer”:
break;
default:
req.setRequestHeader(h.key, h.value);
break;
}
});
},

Leave a comment