I am working on a project to export Word templates. I have about two dozen custom templates and I was able to use the example coding from this thread (https://stackoverflow.com/questions/44525991/dynamics-crm-365-downloading-a-word-document-template-via-a-button-on-the-ribb) to build an interface to download one template at a time when the user checks a box associated with each template. The user now would like to combine selected templates into a single Word file upon export. Is possible to extend the export method (var formdata = "exportType=MergeWordTemplate&selectedRecords=" + encodeURIComponent(JSON.stringify(IDS)) + "&associatedentitytypecode=" + currentEntityTypeCode + "&TemplateId=" + wordTemplateId + "&TemplateType=" + templateType;) to include multiple templates? I've tried a number of different ways without success. I was also not able to find this exportType method defined anywhere. Any help is appreciated.
My current code:
// JavaScript Document
this.ExecuteWordMerge = function (executionContext, wordtemplateid, entitytypecodeint, templatetype, fieldforfilename, filenameoverride, fieldname) {
var formContext = executionContext.getFormContext(); // get formContext
var FieldName = fieldname;
var filecheck = formContext.getAttribute(FieldName).getValue();
if (filecheck == 1) {
try{
Xrm.Page.ui.clearFormNotification("worderror");
var globalContext = Xrm.Utility.getGlobalContext();
var clientUrl = globalContext.getClientUrl();
// Xrm.page is deprecated Xrm.Page.context.getClientUrl()
var funcpath = clientUrl + "/_grid/print/print_data.aspx";
var IDS = formContext.data.entity.getId(); // get the entity id for the current record
if (typeof IDS !== "object") {
var tids = IDS;
IDS = new Array();
IDS.push(tids);
}
var wordTemplateId = wordtemplateid;//"f1f7b994-543b-e711-8106-c4346bac2908" test data;
var currentEntityTypeCode = entitytypecodeint;//"10063" test data;
var templateType = (templatetype || 9940); //9940 is global and 9941 is personal
var fieldForFileName = (fieldforfilename || "");
var formdata = "exportType=MergeWordTemplate&selectedRecords=" + encodeURIComponent(JSON.stringify(IDS)) +
"&associatedentitytypecode=" + currentEntityTypeCode + "&TemplateId=" + wordTemplateId + "&TemplateType=" + templateType;
var req = new XMLHttpRequest();
req.open("POST", funcpath, true);
req.responseType = "arraybuffer";
req.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
req.setRequestHeader("Accept-Language", "en-US,en;q=0.8");
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.onreadystatechange = function () {
if (this.readyState == 4) {/* complete */
req.onreadystatechange = null;
if (this.status >= 200 && this.status <= 299) {//200 range okay
var mimetype = (2 === 2) ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var blob = new Blob([req.response], { type: mimetype });
var fileNameTemplate = req.getResponseHeader('content-disposition').split('filename=')[1].replace(/'/g, "");
var dloadurl = URL.createObjectURL(blob);
var filename = (fieldForFileName !== "" && formContext.getAttribute(fieldForFileName) !== null && formContext.getAttribute(fieldForFileName).getValue() !== "") ?
formContext.getAttribute(fieldForFileName).getValue() : fileNameTemplate;
filename = filenameoverride || filename;
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = dloadurl;
a.download = filename;
a.click();
URL.revokeObjectURL(dloadurl);
//window.location = dloadurl;//we can use just this instead of creating an anchor but we don't get to the name the file
}
else {
formContext.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
}
}
};
req.send(formdata);
}
catch (err) {
formContext.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists. " + err.message, "ERROR", "worderror");
}
}
}