Skip to content

Instantly share code, notes, and snippets.

@johandanforth
Last active February 22, 2021 21:00
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/ae87f35b41164c501ccf8caa7d08bdd9 to your computer and use it in GitHub Desktop.
Save johandanforth/ae87f35b41164c501ccf8caa7d08bdd9 to your computer and use it in GitHub Desktop.
name: Get attachment content
description: ''
host: OUTLOOK
api_set: {}
script:
content: "$(\"#read\").click(read);\n\nfunction read() {\n Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function(asyncResult) {\n getCurrentItem(asyncResult.value);\n });\n}\n\nfunction getItemRestId() {\n if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {\n // itemId is already REST-formatted.\n return Office.context.mailbox.item.itemId;\n } else {\n // Convert to an item ID for API v2.0.\n return Office.context.mailbox.convertToRestId(\n Office.context.mailbox.item.itemId,\n Office.MailboxEnums.RestVersion.v2_0\n );\n }\n}\n\nfunction getCurrentItem(accessToken) {\n // Get the item's REST ID.\n var itemId = getItemRestId();\n \n var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/events/' + itemId + '?expand=attachments';\n\n $.ajax({\n url: getMessageUrl,\n dataType: 'json',\n headers: { 'Authorization': 'Bearer ' + accessToken }\n }).done(function (item) {\n if(item.Attachments.length > 0){\n let att = item.Attachments[0];\n var raw = window.atob(att.ContentBytes);\n console.log(raw)\n let dom = (new DOMParser()).parseFromString(raw, \"text/xml\"); \n //console.log(dom)\n let json = xml2json(dom,\"\")\n //console.log(json)\n let workitem = JSON.parse(json);\n console.log(workitem)\n }\n }).fail(function (error) {\n console.error(error);\n });\n}\n\nfunction getAttachment(itemId, attachmentId, accessToken){\n\n var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/events/' + itemId + \"/attachments/\" + attachmentId;\n\n $.ajax({\n url: getMessageUrl,\n dataType: 'json',\n headers: { 'Authorization': 'Bearer ' + accessToken }\n }).done(function (item) {\n console.log(item)\n\n }).fail(function (error) {\n console.error(error);\n });\n}\n\n\n/*\tThis work is licensed under Creative Commons GNU LGPL License.\n\n\tLicense: http://creativecommons.org/licenses/LGPL/2.1/\n Version: 0.9\n\tAuthor: Stefan Goessner/2006\n\tWeb: http://goessner.net/ \n*/\nfunction xml2json(xml, tab) {\n var X = {\n toObj: function (xml) {\n var o = {};\n if (xml.nodeType == 1) { // element node ..\n if (xml.attributes.length) // element with attributes ..\n for (var i = 0; i < xml.attributes.length; i++)\n o[\"@\" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || \"\").toString();\n if (xml.firstChild) { // element has child nodes ..\n var textChild = 0, cdataChild = 0, hasElementChild = false;\n for (var n = xml.firstChild; n; n = n.nextSibling) {\n if (n.nodeType == 1) hasElementChild = true;\n else if (n.nodeType == 3 && n.nodeValue.match(/[^ \\f\\n\\r\\t\\v]/)) textChild++; // non-whitespace text\n else if (n.nodeType == 4) cdataChild++; // cdata section node\n }\n if (hasElementChild) {\n if (textChild < 2 && cdataChild < 2) { // structured element with evtl. a single text or/and cdata node ..\n X.removeWhite(xml);\n for (var n = xml.firstChild; n; n = n.nextSibling) {\n if (n.nodeType == 3) // text node\n o[\"#text\"] = X.escape(n.nodeValue);\n else if (n.nodeType == 4) // cdata node\n o[\"#cdata\"] = X.escape(n.nodeValue);\n else if (o[n.nodeName]) { // multiple occurence of element ..\n if (o[n.nodeName] instanceof Array)\n o[n.nodeName][o[n.nodeName].length] = X.toObj(n);\n else\n o[n.nodeName] = [o[n.nodeName], X.toObj(n)];\n }\n else // first occurence of element..\n o[n.nodeName] = X.toObj(n);\n }\n }\n else { // mixed content\n if (!xml.attributes.length)\n o = X.escape(X.innerXml(xml));\n else\n o[\"#text\"] = X.escape(X.innerXml(xml));\n }\n }\n else if (textChild) { // pure text\n if (!xml.attributes.length)\n o = X.escape(X.innerXml(xml));\n else\n o[\"#text\"] = X.escape(X.innerXml(xml));\n }\n else if (cdataChild) { // cdata\n if (cdataChild > 1)\n o = X.escape(X.innerXml(xml));\n else\n for (var n = xml.firstChild; n; n = n.nextSibling)\n o[\"#cdata\"] = X.escape(n.nodeValue);\n }\n }\n if (!xml.attributes.length && !xml.firstChild) o = null;\n }\n else if (xml.nodeType == 9) { // document.node\n o = X.toObj(xml.documentElement);\n }\n else\n alert(\"unhandled node type: \" + xml.nodeType);\n return o;\n },\n toJson: function (o, name, ind) {\n var json = name ? (\"\\\"\" + name + \"\\\"\") : \"\";\n if (o instanceof Array) {\n for (var i = 0, n = o.length; i < n; i++)\n o[i] = X.toJson(o[i], \"\", ind + \"\\t\");\n json += (name ? \":[\" : \"[\") + (o.length > 1 ? (\"\\n\" + ind + \"\\t\" + o.join(\",\\n\" + ind + \"\\t\") + \"\\n\" + ind) : o.join(\"\")) + \"]\";\n }\n else if (o == null)\n json += (name && \":\") + \"null\";\n else if (typeof (o) == \"object\") {\n var arr = [];\n for (var m in o)\n arr[arr.length] = X.toJson(o[m], m, ind + \"\\t\");\n json += (name ? \":{\" : \"{\") + (arr.length > 1 ? (\"\\n\" + ind + \"\\t\" + arr.join(\",\\n\" + ind + \"\\t\") + \"\\n\" + ind) : arr.join(\"\")) + \"}\";\n }\n else if (typeof (o) == \"string\")\n json += (name && \":\") + \"\\\"\" + o.toString() + \"\\\"\";\n else\n json += (name && \":\") + o.toString();\n return json;\n },\n innerXml: function (node) {\n var s = \"\"\n if (\"innerHTML\" in node)\n s = node.innerHTML;\n else {\n var asXml = function (n) {\n var s = \"\";\n if (n.nodeType == 1) {\n s += \"<\" + n.nodeName;\n for (var i = 0; i < n.attributes.length; i++)\n s += \" \" + n.attributes[i].nodeName + \"=\\\"\" + (n.attributes[i].nodeValue || \"\").toString() + \"\\\"\";\n if (n.firstChild) {\n s += \">\";\n for (var c = n.firstChild; c; c = c.nextSibling)\n s += asXml(c);\n s += \"</\" + n.nodeName + \">\";\n }\n else\n s += \"/>\";\n }\n else if (n.nodeType == 3)\n s += n.nodeValue;\n else if (n.nodeType == 4)\n s += \"<![CDATA[\" + n.nodeValue + \"]]>\";\n return s;\n };\n for (var c = node.firstChild; c; c = c.nextSibling)\n s += asXml(c);\n }\n return s;\n },\n escape: function (txt) {\n return txt.replace(/[\\\\]/g, \"\\\\\\\\\")\n .replace(/[\\\"]/g, '\\\\\"')\n .replace(/[\\n]/g, '\\\\n')\n .replace(/[\\r]/g, '\\\\r');\n },\n removeWhite: function (e) {\n e.normalize();\n for (var n = e.firstChild; n;) {\n if (n.nodeType == 3) { // text node\n if (!n.nodeValue.match(/[^ \\f\\n\\r\\t\\v]/)) { // pure whitespace text node\n var nxt = n.nextSibling;\n e.removeChild(n);\n n = nxt;\n }\n else\n n = n.nextSibling;\n }\n else if (n.nodeType == 1) { // element node\n X.removeWhite(n);\n n = n.nextSibling;\n }\n else // any other node\n n = n.nextSibling;\n }\n return e;\n }\n };\n if (xml.nodeType == 9) // document node\n xml = xml.documentElement;\n var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, \"\\t\");\n return \"{\\n\" + tab + (tab ? json.replace(/\\t/g, tab) : json.replace(/\\t|\\n/g, \"\")) + \"\\n}\";\n}\n\n"
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