Skip to content

Instantly share code, notes, and snippets.

@johandanforth
Last active February 22, 2021 19:03
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/6b27f19a5cf98d696c104d015fa7651f to your computer and use it in GitHub Desktop.
Save johandanforth/6b27f19a5cf98d696c104d015fa7651f to your computer and use it in GitHub Desktop.
name: Get attachment content
description: Gets the attachment content.
host: OUTLOOK
api_set: {}
script:
content: |
$("#read").click(read);
$("#write").click(write);
var serviceRequest = {
accessToken: '',
ewsUrl: Office.context.mailbox.ewsUrl,
attachments: [],
state: 0,
eventId : '',
attachmentId: ''
};
function read() {
var item = Office.context.mailbox.item;
serviceRequest.eventId = item.itemId; //getItemRestId(item.itemId);
var atts = item.attachments;
if(atts.length === 0)
{
console.log("---- no attachments ----");
return;
}
serviceRequest.attachmentId = atts[0].id;
getAttachmentToken();
}
function getAttachmentToken() {
if (serviceRequest.accessToken == "") {
var options = { asyncContext: { isRest: true } };
Office.context.mailbox.getCallbackTokenAsync(options, tokenCallback);
}
}
function tokenCallback(asyncResult) {
console.log(asyncResult);
if (asyncResult.status === "succeeded") {
serviceRequest.accessToken = asyncResult.value;
//serviceRequest.state = 3;
getAttachment()
} else {
console.error("Error Could not get callback token: " + asyncResult.error.message);
}
}
function getAttachment(){
console.log(serviceRequest.accessToken);
var options = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Origin': '',
'Host': 'api.producthunt.com',
'Authorization': 'Bearer ' + serviceRequest.accessToken
}
}
var restHost = Office.context.mailbox.restUrl;
console.log(restHost)
var getMessageUrl = Office.context.mailbox.restUrl +
'/v2.0/me/messages/' + serviceRequest.eventId;
$.ajax({
url: getMessageUrl,
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + serviceRequest.accessToken }
}).done(function (item) {
// Message is passed in `item`.
var subject = item.Subject;
console.log(subject)
}).fail(function (error) {
// Handle error
console.error(error);
});
// let url = "https://graph.microsoft.com/v1.0/me/calendar/events/" +
// serviceRequest.eventId + "/attachments/" +
// serviceRequest.attachmentId;
// console.log(url);
// fetch(url, options)
// .then(response => response.json())
// .then(data => {
// console.log(data);
// });
}
function getItemRestId(id) {
if (Office.context.mailbox.diagnostics.hostName === 'OutlookWebApp') {
// itemId is already REST-formatted.
return Office.context.mailbox.item.itemId;
} else {
// Convert to an item ID for API v2.0.
return Office.context.mailbox.convertToRestId(id,Office.MailboxEnums.RestVersion.v2_0
);
}
}
function write() {
var item = Office.context.mailbox.item;
var options = { asyncContext: { currentItem: item } };
item.getAttachmentsAsync(options, callback);
function callback(result) {
if (result.value.length > 0) {
for (var i = 0; i < result.value.length; i++) {
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
name: Get item with rest
description: ''
host: OUTLOOK
api_set: {}
script:
content: |+
$("#read").click(read);
function read() {
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function(asyncResult) {
getCurrentItem(asyncResult.value);
});
}
function getItemRestId() {
if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {
// itemId is already REST-formatted.
return Office.context.mailbox.item.itemId;
} else {
// Convert to an item ID for API v2.0.
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}
function getCurrentItem(accessToken) {
// Get the item's REST ID.
var itemId = getItemRestId();
var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/events/' + itemId + '?expand=attachments';
$.ajax({
url: getMessageUrl,
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function (item) {
if(item.HasAttachment){
//getAttachment(itemId, accessToken);
}
}).fail(function (error) {
console.error(error);
});
}
function getAttachment(itemId, attachmentId, accessToken){
var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/events/' + itemId + "/attachments/" + attachmentId;
$.ajax({
url: getMessageUrl,
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function (item) {
console.log(item)
}).fail(function (error) {
console.error(error);
});
}
language: typescript
template:
content: "<section class=\"samples ms-font-m\">\n\n\t<button id=\"read\" class=\"ms-Button\">\n <div class=\"ms-Button-label\">Read mode</div>\n </button>\n\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