id: outlook-notifications-add-getall-remove
name: Work with notification messages
description: Adds different kinds of notification messages, gets all notifications, and replaces and removes an individual notification message.
host: OUTLOOK
api_set:
Mailbox: '1.10'
script:
content: |-
document.getElementById("addProgress").addEventListener("click", addProgress);
document.getElementById("addInformational").addEventListener("click", addInformational);
document.getElementById("addInformationalPersisted").addEventListener("click", addInformationalPersisted);
document.getElementById("addInsight").addEventListener("click", addInsight);
document.getElementById("addError").addEventListener("click", addError);
document.getElementById("getAll").addEventListener("click", getAll);
document.getElementById("replace").addEventListener("click", replace);
document.getElementById("remove").addEventListener("click", remove);
function addProgress() {
// Adds a progress indicator to the mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
const details =
{
type: Office.MailboxEnums.ItemNotificationMessageType.ProgressIndicator,
message: "Progress indicator with id = " + id
};
Office.context.mailbox.item.notificationMessages.addAsync(id, details, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to add progress notification with id = ${id}. Try using a different ID.`);
return;
}
console.log(`Added progress notification with id = ${id}.`);
});
}
function addInformational() {
// Adds an informational notification to the mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
const details =
{
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: "Non-persistent informational notification message with id = " + id,
icon: "PG.Icon.16",
persistent: false
};
Office.context.mailbox.item.notificationMessages.addAsync(id, details, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to add informational notification with id = ${id}. Try using a different ID.`);
return;
}
console.log(`Added informational notification with id = ${id}.`);
});
}
function addInformationalPersisted() {
// Adds a persistent information notification to the mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
const details =
{
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: "Persistent informational notification message with id = " + id,
icon: "PG.Icon.16",
persistent: true
};
Office.context.mailbox.item.notificationMessages.addAsync(id, details, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to add persistent informational notification with id = ${id}. Try using a different ID.`);
return;
}
console.log(`Added persistent informational notification with id = ${id}.`);
});
}
function addInsight() {
// Adds an informational message with actions to the mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
const itemId = Office.context.mailbox.item.itemId;
const details = {
type: Office.MailboxEnums.ItemNotificationMessageType.InsightMessage,
message: "This is an insight notification with id = " + id,
icon: "PG.Icon.16",
actions: [
{
actionText: "Open insight",
actionType: Office.MailboxEnums.ActionType.ShowTaskPane,
// Identify whether the current mail item is in read or compose mode to set the appropriate commandId value.
commandId: (itemId == undefined ? "PG.HelpCommand.Compose" : "PG.HelpCommand.Read"),
contextData: { a: "aValue", b: "bValue" }
}
]
};
Office.context.mailbox.item.notificationMessages.addAsync(id, details, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to add insight notification with id = ${id}. Try using a different ID.`);
return;
}
console.log(`Added insight notification with id = ${id}.`);
});
}
function addError() {
// Adds an error notification to the mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
const details =
{
type: Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage,
message: "Error notification message with id = " + id
};
Office.context.mailbox.item.notificationMessages.addAsync(id, details, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to add error notification with id = ${id}. Try using a different ID.`);
return;
}
console.log(`Added error notification with id = ${id}.`);
});
}
function getAll() {
// Gets all the notification messages and their keys for the current mail item.
Office.context.mailbox.item.notificationMessages.getAllAsync((asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
return;
}
console.log(JSON.stringify(asyncResult.value));
});
}
function replace() {
// Replaces a notification message of a given key with another message.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
Office.context.mailbox.item.notificationMessages.replaceAsync(
id,
{
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: "Notification message with id = " + id + " has been replaced with an informational message.",
icon: "icon2",
persistent: false
},
(result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to replace notification with id = ${id}. ${result.error.message}.`);
return;
}
console.log(`Replaced notification with id = ${id}.`);
});
}
function remove() {
// Removes a notification message from the current mail item.
const id = (document.getElementById("notificationId") as HTMLInputElement).value;
Office.context.mailbox.item.notificationMessages.removeAsync(id, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(`Failed to remove notification with id = ${id}. ${result.error.message}.`);
return;
}
console.log(`Removed notification with id = ${id}.`);
});
}
language: typescript
template:
content: |-
This sample shows how to add different kinds of notification messages, get all, replace, and remove an individual notification message.
Try it out
Add a notification
To add a notification, enter a unique ID for the notification in the text field, then select one of the notification types below.
Note:
You can add a maximum of five notifications per mail item.
You can only add one insight notification to a mail item.
In Outlook on the web and in new Outlook on Windows, you can only add an insight notification to an item in compose mode.
Get all notifications
Replace a notification
To replace a notification with an informational message, enter the ID of the notification you want to replace in the text field, then select Replace notification.
Remove a notification
To remove a notification, enter the ID of the notification you want to remove in the text field, then select Remove notification.