Skip to content

Instantly share code, notes, and snippets.

@johandanforth
Created February 22, 2021 16:59
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 johandanforth/845566fba529e2d379845bdab20649a9 to your computer and use it in GitHub Desktop.
Save johandanforth/845566fba529e2d379845bdab20649a9 to your computer and use it in GitHub Desktop.
Gets the attachment content.
name: Get attachment content
description: Gets the attachment content.
host: OUTLOOK
api_set: {}
script:
content: |
$("#read").click(read);
$("#write").click(write);
var serviceRequest = {
attachmentToken : ""
}
function read() {
var item = Office.context.mailbox.item;
var atts = item.attachments;
console.log(atts[0])
var attid = atts[0].id;
getAttachmentToken();
}
function getAttachmentToken() {
if (serviceRequest.attachmentToken == "") {
var options = { asyncContext: { isRest: true } };
Office.context.mailbox.getCallbackTokenAsync(options, attachmentTokenCallback);
}
}
function attachmentTokenCallback(asyncResult) {
console.log(asyncResult)
if (asyncResult.status === "succeeded") {
// Cache the result from the server.
serviceRequest.attachmentToken = asyncResult.value;
serviceRequest.state = 3;
//testAttachments();
} else {
//showToast("Error", "Could not get callback token: " + asyncResult.error.message);
}
}
function write() {
var item = Office.context.mailbox.item;
var options = { asyncContext: { currentItem: item } };
item.getAttachmentsAsync(options, callback);
function callback(result) {
console.log(result);
if (result.value.length > 0) {
for (var i = 0; i < result.value.length; i++) {
console.log("testing attachment " + (i + 1));
try {
result.asyncContext.currentItem.getAttachmentContentAsync(result.value[i].id, handleAttachmentsCallback);
} catch (ex) {
console.error("exception");
console.error(ex);
}
}
}
}
function handleAttachmentsCallback(result) {
console.log(result.value.format);
console.log(result.value.content);
// Parse string to be a url, an .eml file, a base64-encoded string, or an .icalendar file.
switch (result.value.format) {
case Office.MailboxEnums.AttachmentContentFormat.Base64:
// Handle file attachment.
console.log(result.value.content);
break;
case Office.MailboxEnums.AttachmentContentFormat.Eml:
// Handle email item attachment.
console.log("Attachment is a message.");
break;
case Office.MailboxEnums.AttachmentContentFormat.ICalendar:
// Handle .icalender attachment.
console.log("Attachment is a calendar item.");
break;
case Office.MailboxEnums.AttachmentContentFormat.Url:
// Handle cloud attachment.
console.log("Attachment is a cloud attachment.");
break;
default:
// Handle attachment formats that are not supported.
}
}
}
language: typescript
template:
content: "\n<section class=\"samples ms-font-m\">\n\t\n\t<button id=\"read\" class=\"ms-Button\">\n <div class=\"ms-Button-label\">Read mode</div>\n </button>\n\n\t\t<button id=\"write\" class=\"ms-Button\">\n\t\t <div class=\"ms-Button-label\">Write mode</div>\n\t\t</button>\n</section>"
language: html
style:
content: |
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment