Created
December 15, 2019 20:38
-
-
Save AshV/dbb8e06e6c110c583289edfb92f3ca4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.2/jszip.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script> | |
</head> | |
<body> | |
<button onclick="getAttachmentList()">Download All Attachmenta</button> | |
<script> | |
function getAttachmentList() { | |
var emailId = parent.Xrm.Page.data.entity.getId(); | |
parent.Xrm.Utility.showProgressIndicator("Preparind attachments download..."); | |
parent.Xrm.WebApi | |
.retrieveMultipleRecords("activitymimeattachment", | |
"$select=activitymimeattachmentid,mimetype,filename&$filter=_objectid_value eq " + emailId) | |
.then(listAttachment, function (err) { | |
Xrm.Utility.closeProgressIndicator(); | |
alert("Error occured : " + err); | |
}); | |
} | |
function listAttachment(attachmentList) { | |
attachmentList.entities.forEach((record) => { | |
console.log(record); | |
}); | |
Promise.all( | |
attachmentList.entities.map(record => | |
parent.Xrm.WebApi.retrieveRecord("activitymimeattachment", | |
record.activitymimeattachmentid, | |
"$select=activitymimeattachmentid,mimetype,filename,body") | |
) | |
).then(filesData => { | |
saveToZip("EmailAttachments.zip", filesData) | |
}, (err) => { | |
Xrm.Utility.closeProgressIndicator(); | |
alert("Error occured" + err); | |
}); | |
} | |
function saveToZip(filename, filesData) { | |
const zip = new JSZip() | |
const folder = zip.folder('Files') | |
filesData.forEach((attachment) => { | |
folder.file(attachment.filename, atob(attachment.body), { binary: true }) | |
}); | |
zip.generateAsync({ type: "blob" }) | |
.then(blob => saveAs(blob, filename)) | |
.catch(e => console.log(e)); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment