Skip to content

Instantly share code, notes, and snippets.

@AshV
Created December 15, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshV/dbb8e06e6c110c583289edfb92f3ca4c to your computer and use it in GitHub Desktop.
Save AshV/dbb8e06e6c110c583289edfb92f3ca4c to your computer and use it in GitHub Desktop.
<!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