Introduction Download PDF In Dynamics 365
In this blog, i will show how to download latest file generated in the timeline when click on ribbon workbench button on Dynamics 365
For this i have created a button using ribbon workbench
when click on this button it needs to download the file immediately from the timeline
Code i have used
Call this code from ribbon workbench button
Call this code from ribbon workbench button
function downloadLatestPDFFromTimeline(formContext) {
var entityId = formContext.data.entity.getId();
var globalContext = Xrm.Utility.getGlobalContext();
var requestUrl =
globalContext.getClientUrl() +
“/api/data/v9.1/annotations?$select=filename,documentbody,mimetype&$filter=_objectid_value eq ” +
entityId +
” and filename ne null and endswith(filename, ‘.pdf’)&$orderby=createdon desc&$top=1″;
var req = new XMLHttpRequest();
req.open(“GET”, requestUrl, 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) {
if (this.status === 200) {
var response = JSON.parse(this.responseText);
if (response.value && response.value.length > 0) {
var latestDocument = response.value[0];
var documentUrl = latestDocument.documentbody;
var fileName = latestDocument.filename;
// Create a Blob from the document body
var byteCharacters = atob(documentUrl);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], { type: ‘application/pdf’ });
// Create a download link and trigger the download
var a = document.createElement(‘a’);
a.href = URL.createObjectURL(blob);
a.download = fileName;
a.style.display = ‘none’;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
alert(“No PDF documents found in the timeline for this Quote.”);
}
} else {
alert(“Error retrieving the latest PDF document: ” + this.status + ” – ” + this.statusText);
}
}
};
req.send();
}
Now once i click on the download doc button it will immediately download the latest file from the timeline
For any Help or Queries Contact us on info@crmonce.com or +919014146800.