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
+
0 commit comments