Skip to content

Commit 9e1d205

Browse files
[Outlook] (item multi-select) Create code snippet for loadItemByIdAsync method (#968)
* Add loadItemByIdAsync snippet * Apply suggestion from review Co-authored-by: Elizabeth Samuel <[email protected]> --------- Co-authored-by: Elizabeth Samuel <[email protected]>
1 parent bd1e7c3 commit 9e1d205

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

playlists-prod/outlook.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,15 @@
793793
group: Other Item APIs
794794
api_set:
795795
Mailbox: '1.15'
796+
- id: outlook-other-item-apis-get-loaded-message-properties
797+
name: 'Get properties of a loaded message (Message Compose, Message Read)'
798+
fileName: get-loaded-message-properties.yaml
799+
description: Gets the properties of the currently loaded message.
800+
rawUrl: >-
801+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-loaded-message-properties.yaml
802+
group: Other Item APIs
803+
api_set:
804+
Mailbox: '1.15'
796805
- id: outlook-get-set-isalldayevent
797806
name: Get and set the isAllDayEvent property (Appointment Organizer)
798807
fileName: get-set-isalldayevent.yaml

playlists/outlook.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,15 @@
793793
group: Other Item APIs
794794
api_set:
795795
Mailbox: '1.15'
796+
- id: outlook-other-item-apis-get-loaded-message-properties
797+
name: 'Get properties of a loaded message (Message Compose, Message Read)'
798+
fileName: get-loaded-message-properties.yaml
799+
description: Gets the properties of the currently loaded message.
800+
rawUrl: >-
801+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/90-other-item-apis/get-loaded-message-properties.yaml
802+
group: Other Item APIs
803+
api_set:
804+
Mailbox: '1.15'
796805
- id: outlook-get-set-isalldayevent
797806
name: Get and set the isAllDayEvent property (Appointment Organizer)
798807
fileName: get-set-isalldayevent.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
order: 31
2+
id: outlook-other-item-apis-get-loaded-message-properties
3+
name: 'Get properties of a loaded message (Message Compose, Message Read)'
4+
description: Gets the properties of the currently loaded message.
5+
host: OUTLOOK
6+
api_set:
7+
Mailbox: '1.15'
8+
script:
9+
content: |
10+
let list;
11+
12+
Office.onReady((info) => {
13+
if (info.host === Office.HostType.Outlook) {
14+
list = document.getElementById("selected-items");
15+
16+
// Register an event handler to identify when messages are selected.
17+
Office.context.mailbox.addHandlerAsync(Office.EventType.SelectedItemsChanged, run, (asyncResult) => {
18+
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
19+
console.log(asyncResult.error.message);
20+
return;
21+
}
22+
23+
console.log("Event handler added.");
24+
});
25+
26+
run();
27+
}
28+
});
29+
30+
function run() {
31+
// Clear the list of previously selected messages, if any.
32+
clearList(list);
33+
34+
// Get the subject line and sender's email address of each selected message and log them to a list in the task pane.
35+
Office.context.mailbox.getSelectedItemsAsync((asyncResult) => {
36+
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
37+
console.log(asyncResult.error.message);
38+
return;
39+
}
40+
41+
const selectedItems = asyncResult.value;
42+
43+
getItemInfo(selectedItems);
44+
});
45+
}
46+
47+
// Gets the subject line and sender's email address of each selected message.
48+
async function getItemInfo(selectedItems) {
49+
for (const item of selectedItems) {
50+
addToList(item.subject);
51+
await getSenderEmailAddress(item);
52+
}
53+
}
54+
55+
// Gets the sender's email address of each selected message.
56+
async function getSenderEmailAddress(item) {
57+
const itemId = item.itemId;
58+
await new Promise((resolve) => {
59+
Office.context.mailbox.loadItemByIdAsync(itemId, (result) => {
60+
if (result.status === Office.AsyncResultStatus.Failed) {
61+
console.log(result.error.message);
62+
return;
63+
}
64+
65+
const loadedItem = result.value;
66+
const sender = loadedItem.from.emailAddress;
67+
appendToListItem(sender);
68+
69+
// Unload the current message before processing another selected message.
70+
loadedItem.unloadAsync((asyncResult) => {
71+
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
72+
console.log(asyncResult.error.message);
73+
return;
74+
}
75+
76+
resolve();
77+
});
78+
});
79+
});
80+
}
81+
82+
// Clears the list in the task pane.
83+
function clearList(list) {
84+
while (list.firstChild) {
85+
list.removeChild(list.firstChild);
86+
}
87+
}
88+
89+
// Adds an item to a list in the task pane.
90+
function addToList(item) {
91+
const listItem = document.createElement("li");
92+
listItem.textContent = item;
93+
list.appendChild(listItem);
94+
}
95+
96+
// Appends data to the last item of the list in the task pane.
97+
function appendToListItem(data) {
98+
const listItem = list.lastChild;
99+
listItem.textContent += ` (${data})`;
100+
}
101+
language: typescript
102+
template:
103+
content: |-
104+
<section class="ms-Fabric ms-font-m">
105+
<p>This sample shows how to get the properties of the currently loaded message.</p>
106+
<p><b>Required mode</b>: Message Compose, Message Read</p>
107+
</section>
108+
<section class="ms-Fabric samples ms-font-m">
109+
<h3>Try it out</h3>
110+
<ol>
111+
<li>
112+
<p>Turn on the Reading Pane in Outlook. For guidance, see <a
113+
href="https://support.microsoft.com/office/2fd687ed-7fc4-4ae3-8eab-9f9b8c6d53f0" target="_blank">Use
114+
and configure the Reading Pane to preview messages</a>.</p>
115+
</li>
116+
<li>
117+
<p>Select a message from your mailbox. To select multiple messages, hold <kbd>Ctrl</kbd> (Windows) while
118+
selecting each message. You can select a maximum of 100 messages at a time.</p>
119+
</li>
120+
</ol>
121+
<p>The subject and email address of the sender are automatically logged to the "Selected messages" section of the
122+
task pane.</p>
123+
<p>To learn more about the item multi-select feature, see <a
124+
href="https://learn.microsoft.com/office/dev/add-ins/outlook/item-multi-select" target="_blank">Activate
125+
your Outlook add-in on multiple messages</a>.</p>
126+
<h3>Selected messages</h3>
127+
<ul id="selected-items"></ul>
128+
</section>
129+
language: html
130+
style:
131+
content: |-
132+
section.samples {
133+
margin-top: 20px;
134+
}
135+
136+
section.samples .ms-Button, section.setup .ms-Button {
137+
display: block;
138+
margin-bottom: 5px;
139+
margin-left: 20px;
140+
min-width: 80px;
141+
}
142+
language: css
143+
libraries: |
144+
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
145+
@types/office-js
146+
147+
[email protected]/dist/css/fabric.min.css
148+
[email protected]/dist/css/fabric.components.min.css
149+
150+
[email protected]/client/core.min.js
151+
@types/core-js
152+
153+
154+
91 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -10934,6 +10934,37 @@
1093410934

1093510935
console.log(result.value);
1093610936
});
10937+
'Office.Mailbox#loadItemByIdAsync:member(1)':
10938+
- >-
10939+
// Link to full sample:
10940+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-loaded-message-properties.yaml
10941+
10942+
10943+
async function getSenderEmailAddress(item) {
10944+
const itemId = item.itemId;
10945+
await new Promise((resolve) => {
10946+
Office.context.mailbox.loadItemByIdAsync(itemId, (result) => {
10947+
if (result.status === Office.AsyncResultStatus.Failed) {
10948+
console.log(result.error.message);
10949+
return;
10950+
}
10951+
10952+
const loadedItem = result.value;
10953+
const sender = loadedItem.from.emailAddress;
10954+
appendToListItem(sender);
10955+
10956+
// Unload the current message before processing another selected message.
10957+
loadedItem.unloadAsync((asyncResult) => {
10958+
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
10959+
console.log(asyncResult.error.message);
10960+
return;
10961+
}
10962+
10963+
resolve();
10964+
});
10965+
});
10966+
});
10967+
}
1093710968
'Office.Mailbox#makeEwsRequestAsync:member(1)':
1093810969
- >-
1093910970
// Link to full sample:

view-prod/outlook.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"outlook-get-item-class-async": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-item-class-async.yaml",
8484
"outlook-other-item-apis-item-id-compose": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/item-id-compose.yaml",
8585
"outlook-send-async": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/send-async.yaml",
86+
"outlook-other-item-apis-get-loaded-message-properties": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-loaded-message-properties.yaml",
8687
"outlook-get-set-isalldayevent": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/99-preview-apis/get-set-isalldayevent.yaml",
8788
"outlook-set-displayed-body-subject": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/99-preview-apis/set-displayed-body-subject.yaml"
8889
}

view/outlook.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"outlook-get-item-class-async": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/90-other-item-apis/get-item-class-async.yaml",
8484
"outlook-other-item-apis-item-id-compose": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/90-other-item-apis/item-id-compose.yaml",
8585
"outlook-send-async": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/90-other-item-apis/send-async.yaml",
86+
"outlook-other-item-apis-get-loaded-message-properties": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/90-other-item-apis/get-loaded-message-properties.yaml",
8687
"outlook-get-set-isalldayevent": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/99-preview-apis/get-set-isalldayevent.yaml",
8788
"outlook-set-displayed-body-subject": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/outlook/99-preview-apis/set-displayed-body-subject.yaml"
8889
}

0 commit comments

Comments
 (0)