Skip to content

Commit 0e9eaa3

Browse files
[Outlook] (Body API) Document bodyMode property (#5139)
* Organize layout and document bodyMode property * Update TOC * Fix setting * Update ms.date * Apply suggestion from review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 78b121a commit 0e9eaa3

File tree

2 files changed

+112
-11
lines changed

2 files changed

+112
-11
lines changed

docs/outlook/insert-data-in-the-body.md

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,74 @@
11
---
2-
title: Insert data in the body when composing an appointment or message in Outlook
3-
description: Learn how to insert data into the body of an appointment or message in an Outlook add-in.
4-
ms.date: 08/09/2023
2+
title: Get or set the body of a message or appointment in Outlook
3+
description: Learn how to get or insert data into the body of an appointment or message of an Outlook add-in.
4+
ms.date: 06/03/2025
55
ms.topic: how-to
66
ms.localizationpriority: medium
77
---
88

9-
# Insert data in the body when composing an appointment or message in Outlook
9+
# Get or set the body of a message or appointment in Outlook
1010

11-
Use the asynchronous methods ([Body.getAsync](/javascript/api/outlook/office.body#outlook-office-body-getasync-member(1)), [Body.getTypeAsync](/javascript/api/outlook/office.body#outlook-office-body-gettypeasync-member(1)), [Body.prependAsync](/javascript/api/outlook/office.body#outlook-office-body-prependasync-member(1)), [Body.setAsync](/javascript/api/outlook/office.body#outlook-office-body-setasync-member(1)) and [Body.setSelectedDataAsync](/javascript/api/outlook/office.body#outlook-office-body-setselecteddataasync-member(1))) to get the body type and insert data in the body of an appointment or message being composed. These asynchronous methods are only available to compose add-ins. To use these methods, make sure you have set up the add-in manifest appropriately so that Outlook activates your add-in in compose forms, as described in [Create Outlook add-ins for compose forms](compose-scenario.md).
11+
Call the [Body](/javascript/api/outlook/office.body) API on a message or appointment to retrieve content, determine its format, or update content. With the available Body methods, you can customize signatures depending on mail item recipients or add disclaimers for legal purposes.
12+
13+
Select the applicable tab to learn how to get or set the body of a mail item.
14+
15+
# [Get body](#tab/get)
16+
17+
You can get the body of a message or appointment in both read and compose modes. To retrieve the body of a mail item, call [Office.context.mailbox.item.body.getAsync](/javascript/api/outlook/office.body#outlook-office-body-getasync-member(1)). When you call the `getAsync` method, you must specify the format for the returned body in the `coercionType` parameter. For example, you can get the body in HTML or plain text format.
18+
19+
The following example gets the body of an item in HTML format.
20+
21+
```javascript
22+
// Get the current body of the message or appointment.
23+
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
24+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
25+
console.log(`Failed to get body: ${bodyResult.error.message}`);
26+
return;
27+
}
28+
29+
const body = bodyResult.value;
30+
31+
// Perform additional operations here.
32+
});
33+
```
34+
35+
## Get the body of message replies in Outlook on the web or the new Outlook on Windows
36+
37+
In Outlook on the web and the [new Outlook on Windows](https://support.microsoft.com/office/656bb8d9-5a60-49b2-a98b-ba7822bc7627), users can organize their messages as conversations or individual messages in **Settings** > **Mail** > **Layout** > **Message organization**. This setting affects how much of a message's body is displayed to the user, particularly in conversation threads with multiple messages. Depending on the setting, the contents of the entire conversation thread or just the current message is displayed. For more information on the **Message Organization** setting, see [Change how the message list is displayed in Outlook](https://support.microsoft.com/office/57fe0cd8-e90b-4b1b-91e4-a0ba658c0042).
38+
39+
When you call `Office.context.mailbox.item.body.getAsync` on a message reply, the entire body of a conversation thread is returned. If you want the returned body to reflect the user's **Message Organization** setting, you can specify the [bodyMode](/javascript/api/outlook/office.mailboxenums.bodymode) option in the `getAsync` call. The following table lists the portion of the body returned depending on the `bodyMode` configuration.
40+
41+
| bodyMode configuration | Effect on body |
42+
| ----- | ----- |
43+
| `bodyMode` isn't specified in the `getAsync` call | The entire body of the conversation thread is returned. |
44+
| `bodyMode` is set to `Office.MailboxEnums.BodyMode.FullBody` | The entire body of the conversation thread is returned. |
45+
| `bodyMode` is set to `Office.MailboxEnums.BodyMode.HostConfig` | If **Message Organization** is set to **Group messages by conversation** > **All messages from the selected conversation** or **Show email grouped by conversation** > **Newest on top**/**Newest on bottom**, only the body of the current reply is returned.<br><br>If **Message Organization** is set to **Individual messages: Do not group messages** > **Only a single message** or **Show email as individual messages**, the entire body of the conversation thread is returned. |
46+
47+
> [!NOTE]
48+
> The `bodyMode` option is ignored in Outlook on Windows (classic), on Mac, and on mobile devices.
49+
50+
The following example specifies the `bodyMode` option to honor the user's message setting.
51+
52+
```javascript
53+
Office.context.mailbox.item.body.getAsync(
54+
Office.CoercionType.Html,
55+
{ bodyMode: Office.MailboxEnums.BodyMode.HostConfig },
56+
(bodyResult) => {
57+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
58+
console.log(`Failed to get body: ${bodyResult.error.message}`);
59+
return;
60+
}
61+
62+
const body = bodyResult.value;
63+
64+
// Perform additional operations here.
65+
}
66+
);
67+
```
68+
69+
# [Set body](#tab/set)
70+
71+
Use the asynchronous methods ([Body.getAsync](/javascript/api/outlook/office.body#outlook-office-body-getasync-member(1)), [Body.getTypeAsync](/javascript/api/outlook/office.body#outlook-office-body-gettypeasync-member(1)), [Body.prependAsync](/javascript/api/outlook/office.body#outlook-office-body-prependasync-member(1)), [Body.setAsync](/javascript/api/outlook/office.body#outlook-office-body-setasync-member(1)) and [Body.setSelectedDataAsync](/javascript/api/outlook/office.body#outlook-office-body-setselecteddataasync-member(1))) to get the body type then insert data in the body of an appointment or message being composed. These asynchronous methods are only available to compose add-ins. To use these methods, make sure you have set up the add-in manifest appropriately so that Outlook activates your add-in in compose forms, as described in [Create Outlook add-ins for compose forms](compose-scenario.md).
1272

1373
In Outlook, a user can create a message in text, HTML, or Rich Text Format (RTF), and can create an appointment in HTML format. Before inserting data, you must first verify the supported item format by calling `getTypeAsync`, as you may need to take additional steps. The value that `getTypeAsync` returns depends on the original item format, as well as the support of the device operating system and application to edit in HTML format. Once you've verified the item format, set the `coercionType` parameter of `prependAsync` or `setSelectedDataAsync` accordingly to insert the data, as shown in the following table. If you don't specify an argument, `prependAsync` and `setSelectedDataAsync` assume the data to insert is in text format.
1474

@@ -159,11 +219,52 @@ function prependItemBody() {
159219
}
160220
```
161221

222+
## Set the body of message replies in Outlook on the web or the new Outlook on Windows
223+
224+
In Outlook on the web and the [new Outlook on Windows](https://support.microsoft.com/office/656bb8d9-5a60-49b2-a98b-ba7822bc7627), users can organize their messages as conversations or individual messages in **Settings** > **Mail** > **Layout** > **Message organization**. This setting affects how much of a message's body is displayed to the user, particularly in conversation threads with multiple messages. Depending on the setting, the contents of the entire conversation thread or just the current message is displayed. For more information on the **Message Organization** setting, see [Change how the message list is displayed in Outlook](https://support.microsoft.com/office/57fe0cd8-e90b-4b1b-91e4-a0ba658c0042).
225+
226+
When you call `Office.context.mailbox.item.body.setAsync` on a message reply, the entire body of a conversation thread is replaced with the text you specify. If you want to honor the user's **Message Organization** setting and only replace the body of the current reply, you can specify the [bodyMode](/javascript/api/outlook/office.mailboxenums.bodymode) option in the `setAsync` call. The following table lists the `bodyMode` configurations and how each affects the message body being set.
227+
228+
| bodyMode configuration | Effect on body |
229+
| ----- | ----- |
230+
| `bodyMode` isn't specified in the `setAsync` call | The entire body of the conversation thread is replaced. This applies even if a user's messages are organized by conversation. In this scenario, the user's setting is temporarily changed to **Individual messages: Do not group messages** > **Only a single message** or **Show email as individual messages** during the `setAsync` call. A notification is shown to the user to alert them to this change. Once the call completes, the user's setting is reinstated. |
231+
| `bodyMode` is set to `Office.MailboxEnums.BodyMode.FullBody` | The entire body of the conversation thread is replaced. This applies even if a user's messages are organized by conversation. In this scenario, the user's setting is temporarily changed to **Individual messages: Do not group messages** > **Only a single message** or **Show email as individual messages** during the `setAsync` call. A notification is shown to the user to alert them to this change. Once the call completes, the user's setting is reinstated. |
232+
| `bodyMode` is set to `Office.MailboxEnums.BodyMode.HostConfig` | If **Message Organization** is set to **Group messages by conversation** > **All messages from the selected conversation** or **Show email grouped by conversation** > **Newest on top**/**Newest on bottom**, only the body of the current reply is replaced.<br><br>If **Message Organization** is set to **Individual messages: Do not group messages** > **Only a single message** or **Show email as individual messages**, the entire body of the conversation thread is replaced. |
233+
234+
> [!NOTE]
235+
> The `bodyMode` option is ignored in Outlook on Windows (classic), on Mac, and on mobile devices.
236+
237+
The following example specifies the `bodyMode` option to honor the user's message setting.
238+
239+
```javascript
240+
Office.context.mailbox.item.body.setAsync(
241+
"This text replaces the body of the message.",
242+
{
243+
coercionType: Office.CoercionType.Html,
244+
bodyMode: Office.MailboxEnums.BodyMode.HostConfig
245+
},
246+
(bodyResult) => {
247+
if (bodyResult.status === Office.AsyncResultStatus.Failed) {
248+
console.log(`Failed to set body: ${bodyResult.error.message}`);
249+
return;
250+
}
251+
252+
console.log("Successfully replaced the body of the message.");
253+
}
254+
);
255+
```
256+
257+
---
258+
259+
## Try code samples in Script Lab
260+
261+
Get the [Script Lab for Outlook add-in](https://appsource.microsoft.com/product/office/wa200001603) and try out the item body code samples to see the get and set APIs in action. To learn more about Script Lab, see [Explore Office JavaScript API using Script Lab](../overview/explore-with-script-lab.md).
262+
162263
## See also
163264

164265
- [Create Outlook add-ins for compose forms](compose-scenario.md)
165266
- [Asynchronous programming in Office Add-ins](../develop/asynchronous-programming-in-office-add-ins.md)
166-
- [Get, set, or add recipients when composing an appointment or message in Outlook](get-set-or-add-recipients.md)
167-
- [Get or set the subject when composing an appointment or message in Outlook](get-or-set-the-subject.md)
168-
- [Get or set the location when composing an appointment in Outlook](get-or-set-the-location-of-an-appointment.md)
169-
- [Get or set the time when composing an appointment in Outlook](get-or-set-the-time-of-an-appointment.md)
267+
- [Prepend or append content to a message or appointment body on send](append-on-send.md)
268+
- [Limits for activation and JavaScript API for Outlook add-ins](limits-for-activation-and-javascript-api-for-outlook-add-ins.md)
269+
- [Get, set, or add recipients when composing an appointment or message in Outlook](get-set-or-add-recipients.md)
270+
- [Get or set the subject when composing an appointment or message in Outlook](get-or-set-the-subject.md)

docs/toc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ items:
816816
href: outlook/web-services.md
817817
- name: Manage notifications
818818
href: outlook/notifications.md
819+
- name: Get or set body
820+
href: outlook/insert-data-in-the-body.md
819821
- name: Get or set categories
820822
href: outlook/categories.md
821823
- name: Get or set internet headers
@@ -838,8 +840,6 @@ items:
838840
href: outlook/add-and-remove-attachments-to-an-item-in-a-compose-form.md
839841
- name: Get and set recipients
840842
href: outlook/get-set-or-add-recipients.md
841-
- name: Insert data in the body
842-
href: outlook/insert-data-in-the-body.md
843843
- name: Get and set subject
844844
href: outlook/get-or-set-the-subject.md
845845
- name: Get and set times

0 commit comments

Comments
 (0)