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);
}
}