id: outlook-item-custom-properties-load-set-get-save
name: Work with item custom properties
description: Gets the custom properties that the add-in placed on the current item, sets a new one, gets it, removes it, and saves all custom properties back to the item.
host: OUTLOOK
api_set:
Mailbox: '1.1'
script:
content: |-
let customProps;
document.getElementById("load").addEventListener("click", load);
document.getElementById("get").addEventListener("click", get);
document.getElementById("get-all").addEventListener("click", getAll);
document.getElementById("set").addEventListener("click", set);
document.getElementById("remove").addEventListener("click", remove);
document.getElementById("save").addEventListener("click", save);
function load() {
Office.context.mailbox.item.loadCustomPropertiesAsync((result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.error(`loadCustomPropertiesAsync failed with message ${result.error.message}`);
return;
}
customProps = result.value;
console.log("Loaded the CustomProperties object.");
});
}
function get() {
const propertyName = (document.getElementById("get-property-name") as HTMLInputElement).value;
const propertyValue = customProps.get(propertyName);
console.log(`The value of custom property "${propertyName}" is "${propertyValue}".`);
}
function getAll() {
let allCustomProps;
if (Office.context.requirements.isSetSupported("Mailbox", "1.9")) {
allCustomProps = customProps.getAll();
} else {
allCustomProps = customProps["rawData"];
}
console.log(allCustomProps);
}
function set() {
const propertyName = (document.getElementById("set-property-name") as HTMLInputElement).value;
const propertyValue = (document.getElementById("property-value") as HTMLInputElement).value;
customProps.set(propertyName, propertyValue);
console.log(`Custom property "${propertyName}" set to value "${propertyValue}".`);
}
function remove() {
const propertyName = (document.getElementById("remove-property-name") as HTMLInputElement).value;
customProps.remove(propertyName);
console.log(`Custom property "${propertyName}" removed.`);
}
function save() {
customProps.saveAsync((result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.error(`saveAsync failed with message ${result.error.message}`);
return;
}
console.log(`Custom properties saved with status: ${result.status}`);
});
}
language: typescript
template:
content: |-
This sample shows how to set, save, and get the custom per-item properties of an add-in.