Skip to main content

Notifications

Microsoft Dynamics CRM forum
Unanswered

How to convert SSRS report in word format using JavaScript?

Posted on by 5

Hi Team. I want to generate SSRS Reports in Microsoft Word document format and attach as a Note in custom entity. by using JavaScript. I have tried below code, but it did not work. Can any one suggests JavaScript for this.

function fnConvertReportToWord(projectId, formContext, reportGuid) {

let queryString = globalContext.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=public&ContentDisposition=OnlyHtmlInline&Format=DOCX";

var retrieveEntityReq = new XMLHttpRequest();
retrieveEntityReq.open("GET", queryString, true);
retrieveEntityReq.setRequestHeader("Accept", "*/*");
retrieveEntityReq.responseType = "arraybuffer";
retrieveEntityReq.onreadystatechange = function () { // This is the call-back function.
if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
var binary = "";
var bytes = new Uint8Array(this.response);
for (var i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
//This is the base64 Word formatted string
var base64WordString = btoa(binary);
CreateConsolidateWordNoteUnderProject(base64WordString, projectId, formContext, reportGuid);

}
};
//This statement sends the request for execution asynchronously. Callback function will be called on completion of the request.
retrieveEntityReq.send();

}

function CreateConsolidateWordNoteUnderProject(data, projectId, formContext, reportGuid) {
try {

var fileName = "Test";

//deleteAttachments(fileName);
var entity = {};
entity.documentbody = data;
entity.objecttypecode = 'new_customerEntity';
entity["objectid_customerEntity@odata.bind"] = "/new_customerEntitys(" + projectId + ")";
entity.subject = fileName;
entity.filename = fileName;
//entity.mimetype = "application/pdf";
//entity.mimetype = "application/msword";
entity.mimetype = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
var globalContext = Xrm.Utility.getGlobalContext();
var req = new XMLHttpRequest();
req.open("POST", globalContext.getClientUrl() + "/api/data/v8.2/annotations", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
formContext.data.refresh(true);

} else {
console.log(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
catch (error) {
HideLoading();
Console.log("Error in createConsolidateNote JS Error: " + error.message);
}
}

Categories:

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans