Skip to content

Commit e9cade9

Browse files
[Outlook] (attachment) Update snippet (#962)
1 parent a034dfc commit e9cade9

File tree

3 files changed

+125
-103
lines changed

3 files changed

+125
-103
lines changed

samples/outlook/20-item-body/add-inline-base64-image.yaml

+53-28
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,60 @@ host: OUTLOOK
66
api_set:
77
Mailbox: '1.8'
88
script:
9-
content: |
10-
$("#add-image").on("click", addImage);
9+
content: |-
10+
$("#prepend-image").on("click", prependImage);
11+
$("#append-image").on("click", appendImage);
12+
13+
const base64String =
14+
"iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";
15+
16+
function prependImage() {
17+
// Insert the Base64-encoded image to the beginning of the body.
18+
Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", { isInline: true }, (attachmentResult) => {
19+
if (attachmentResult.status === Office.AsyncResultStatus.Failed) {
20+
console.log(`Failed to attach file: ${attachmentResult.error.message}`);
21+
return;
22+
}
23+
24+
Office.context.mailbox.item.body.prependAsync('<img src="cid:sample.png" />', { coercionType: Office.CoercionType.Html }, (prependResult) => {
25+
if (prependResult.status === Office.AsyncResultStatus.Failed) {
26+
console.log(`Failed to prepend image to body: ${attachmentResult.error.message}`);
27+
return;
28+
}
1129
12-
function addImage() {
13-
const mailItem = Office.context.mailbox.item;
14-
const base64String =
15-
"iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";
30+
console.log("Inline Base64-encoded image added to the beginning of the body.");
31+
})
32+
});
33+
}
1634
35+
function appendImage() {
1736
// Get the current body of the message or appointment.
18-
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
19-
if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
20-
// Insert the Base64-encoded image to the beginning of the body.
21-
const options = { isInline: true, asyncContext: bodyResult.value };
22-
mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
23-
if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
24-
let body = attachResult.asyncContext;
25-
body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);
37+
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
38+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
39+
console.log(`Failed to get body: ${bodyResult.error.message}`);
40+
return;
41+
}
42+
43+
// Add the Base64-encoded image to the end of the body.
44+
const options = { isInline: true, asyncContext: bodyResult.value };
45+
Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
46+
if (attachResult.status === Office.AsyncResultStatus.Failed) {
47+
console.log(`Failed to attach file: ${attachResult.error.message}`);
48+
return;
49+
}
2650
27-
mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
28-
if (setResult.status === Office.AsyncResultStatus.Succeeded) {
29-
console.log("Inline Base64-encoded image added to the body.");
30-
} else {
31-
console.log(setResult.error.message);
32-
}
33-
});
34-
} else {
35-
console.log(attachResult.error.message);
51+
let body = attachResult.asyncContext;
52+
body += '<img src="cid:sample.png" />';
53+
54+
Office.context.mailbox.item.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
55+
if (setResult.status === Office.AsyncResultStatus.Failed) {
56+
console.log(`Failed to set body: ${setResult.error.message}`);
57+
return;
3658
}
59+
60+
console.log("Inline Base64-encoded image added to the end of the body.");
3761
});
38-
} else {
39-
console.log(bodyResult.error.message);
40-
}
62+
});
4163
});
4264
}
4365
language: typescript
@@ -50,8 +72,11 @@ template:
5072
5173
<section class="ms-Fabric samples ms-font-m">
5274
<h3>Try it out</h3>
53-
<button id="add-image" class="ms-Button">
54-
<span class="ms-Button-label">Add an inline Base64-encoded image</span>
75+
<button id="prepend-image" class="ms-Button">
76+
<span class="ms-Button-label">Prepend image to body</span>
77+
</button>
78+
<button id="append-image" class="ms-Button">
79+
<span class="ms-Button-label">Append image to body</span>
5580
</button>
5681
</section>
5782
language: html
-77 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

+72-75
Original file line numberDiff line numberDiff line change
@@ -9588,36 +9588,35 @@
95889588
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/add-inline-base64-image.yaml
95899589

95909590

9591-
const mailItem = Office.context.mailbox.item;
9591+
// Get the current body of the message or appointment.
95929592

9593-
const base64String =
9594-
"iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";
9593+
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html,
9594+
(bodyResult) => {
9595+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
9596+
console.log(`Failed to get body: ${bodyResult.error.message}`);
9597+
return;
9598+
}
95959599

9596-
// Get the current body of the message or appointment.
9600+
// Add the Base64-encoded image to the end of the body.
9601+
const options = { isInline: true, asyncContext: bodyResult.value };
9602+
Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9603+
if (attachResult.status === Office.AsyncResultStatus.Failed) {
9604+
console.log(`Failed to attach file: ${attachResult.error.message}`);
9605+
return;
9606+
}
95979607

9598-
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
9599-
if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
9600-
// Insert the Base64-encoded image to the beginning of the body.
9601-
const options = { isInline: true, asyncContext: bodyResult.value };
9602-
mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9603-
if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
9604-
let body = attachResult.asyncContext;
9605-
body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);
9606-
9607-
mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9608-
if (setResult.status === Office.AsyncResultStatus.Succeeded) {
9609-
console.log("Inline Base64-encoded image added to the body.");
9610-
} else {
9611-
console.log(setResult.error.message);
9612-
}
9613-
});
9614-
} else {
9615-
console.log(attachResult.error.message);
9608+
let body = attachResult.asyncContext;
9609+
body += '<img src="cid:sample.png" />';
9610+
9611+
Office.context.mailbox.item.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9612+
if (setResult.status === Office.AsyncResultStatus.Failed) {
9613+
console.log(`Failed to set body: ${setResult.error.message}`);
9614+
return;
96169615
}
9616+
9617+
console.log("Inline Base64-encoded image added to the end of the body.");
96179618
});
9618-
} else {
9619-
console.log(bodyResult.error.message);
9620-
}
9619+
});
96219620
});
96229621
'Office.Body#appendOnSendAsync:member(1)':
96239622
- >-
@@ -9656,36 +9655,35 @@
96569655
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/add-inline-base64-image.yaml
96579656

96589657

9659-
const mailItem = Office.context.mailbox.item;
9658+
// Get the current body of the message or appointment.
96609659

9661-
const base64String =
9662-
"iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";
9660+
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html,
9661+
(bodyResult) => {
9662+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
9663+
console.log(`Failed to get body: ${bodyResult.error.message}`);
9664+
return;
9665+
}
96639666

9664-
// Get the current body of the message or appointment.
9667+
// Add the Base64-encoded image to the end of the body.
9668+
const options = { isInline: true, asyncContext: bodyResult.value };
9669+
Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9670+
if (attachResult.status === Office.AsyncResultStatus.Failed) {
9671+
console.log(`Failed to attach file: ${attachResult.error.message}`);
9672+
return;
9673+
}
96659674

9666-
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
9667-
if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
9668-
// Insert the Base64-encoded image to the beginning of the body.
9669-
const options = { isInline: true, asyncContext: bodyResult.value };
9670-
mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9671-
if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
9672-
let body = attachResult.asyncContext;
9673-
body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);
9674-
9675-
mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9676-
if (setResult.status === Office.AsyncResultStatus.Succeeded) {
9677-
console.log("Inline Base64-encoded image added to the body.");
9678-
} else {
9679-
console.log(setResult.error.message);
9680-
}
9681-
});
9682-
} else {
9683-
console.log(attachResult.error.message);
9675+
let body = attachResult.asyncContext;
9676+
body += '<img src="cid:sample.png" />';
9677+
9678+
Office.context.mailbox.item.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9679+
if (setResult.status === Office.AsyncResultStatus.Failed) {
9680+
console.log(`Failed to set body: ${setResult.error.message}`);
9681+
return;
96849682
}
9683+
9684+
console.log("Inline Base64-encoded image added to the end of the body.");
96859685
});
9686-
} else {
9687-
console.log(bodyResult.error.message);
9688-
}
9686+
});
96899687
});
96909688
'Office.Body#getTypeAsync:member(1)':
96919689
- >-
@@ -9775,36 +9773,35 @@
97759773
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/add-inline-base64-image.yaml
97769774

97779775

9778-
const mailItem = Office.context.mailbox.item;
9776+
// Get the current body of the message or appointment.
97799777

9780-
const base64String =
9781-
"iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";
9778+
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html,
9779+
(bodyResult) => {
9780+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
9781+
console.log(`Failed to get body: ${bodyResult.error.message}`);
9782+
return;
9783+
}
97829784

9783-
// Get the current body of the message or appointment.
9785+
// Add the Base64-encoded image to the end of the body.
9786+
const options = { isInline: true, asyncContext: bodyResult.value };
9787+
Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9788+
if (attachResult.status === Office.AsyncResultStatus.Failed) {
9789+
console.log(`Failed to attach file: ${attachResult.error.message}`);
9790+
return;
9791+
}
97849792

9785-
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
9786-
if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
9787-
// Insert the Base64-encoded image to the beginning of the body.
9788-
const options = { isInline: true, asyncContext: bodyResult.value };
9789-
mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
9790-
if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
9791-
let body = attachResult.asyncContext;
9792-
body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);
9793-
9794-
mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9795-
if (setResult.status === Office.AsyncResultStatus.Succeeded) {
9796-
console.log("Inline Base64-encoded image added to the body.");
9797-
} else {
9798-
console.log(setResult.error.message);
9799-
}
9800-
});
9801-
} else {
9802-
console.log(attachResult.error.message);
9793+
let body = attachResult.asyncContext;
9794+
body += '<img src="cid:sample.png" />';
9795+
9796+
Office.context.mailbox.item.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
9797+
if (setResult.status === Office.AsyncResultStatus.Failed) {
9798+
console.log(`Failed to set body: ${setResult.error.message}`);
9799+
return;
98039800
}
9801+
9802+
console.log("Inline Base64-encoded image added to the end of the body.");
98049803
});
9805-
} else {
9806-
console.log(bodyResult.error.message);
9807-
}
9804+
});
98089805
});
98099806
'Office.Body#setSelectedDataAsync:member(1)':
98109807
- >-

0 commit comments

Comments
 (0)