Office.CustomProperties interface

The CustomProperties object represents custom properties that are specific to a particular item and specific to a mail add-in for Outlook. For example, there might be a need for a mail add-in to save some data that is specific to the current email message that activated the add-in. If the user revisits the same message in the future and activates the mail add-in again, the add-in will be able to retrieve the data that had been saved as custom properties. Important: The maximum length of a CustomProperties JSON object is 2500 characters.

Because Outlook on Mac doesn't cache custom properties, if the user's network goes down, mail add-ins cannot access their custom properties.

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

Methods

get(name)

Returns the value of the specified custom property.

getAll()

Returns an object with all custom properties in a collection of name/value pairs. The following are equivalent.

customProps.get("name")

var dictionary = customProps.getAll(); dictionary["name"]

You can iterate through the dictionary object to discover all names and values.

remove(name)

Removes the specified property from the custom property collection.

To make the removal of the property permanent, you must call the saveAsync method of the CustomProperties object.

saveAsync(callback, asyncContext)

Saves item-specific custom properties to the server.

You must call the saveAsync method to persist any changes made with the set method or the remove method of the CustomProperties object. The saving action is asynchronous.

It's a good practice to have your callback function check for and handle errors from saveAsync. In particular, a read add-in can be activated while the user is in a connected state in a read form, and subsequently the user becomes disconnected. If the add-in calls saveAsync while in the disconnected state, saveAsync would return an error. Your callback method should handle this error accordingly.

saveAsync(asyncContext)

Saves item-specific custom properties to the server.

You must call the saveAsync method to persist any changes made with the set method or the remove method of the CustomProperties object. The saving action is asynchronous.

It's a good practice to have your callback function check for and handle errors from saveAsync. In particular, a read add-in can be activated while the user is in a connected state in a read form, and subsequently the user becomes disconnected. If the add-in calls saveAsync while in the disconnected state, saveAsync would return an error. Your callback method should handle this error accordingly.

set(name, value)

Sets the specified property to the specified value.

The set method sets the specified property to the specified value. You must use the saveAsync method to save the property to the server.

The set method creates a new property if the specified property does not already exist; otherwise, the existing value is replaced with the new value. The value parameter can be of any type; however, it is always passed to the server as a string.

Method Details

get(name)

Returns the value of the specified custom property.

get(name: string): any;

Parameters

name

string

The name of the custom property to be returned.

Returns

any

The value of the specified custom property.

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/15-item-custom-properties/load-set-get-save.yaml
var propertyName = $("#propertyName").val();
var propertyValue = customProps.get(propertyName);
$("#propertyValue").val(propertyValue);
console.log(`The value of custom property "${propertyName}" is "${propertyValue}".`);

getAll()

Returns an object with all custom properties in a collection of name/value pairs. The following are equivalent.

customProps.get("name")

var dictionary = customProps.getAll(); dictionary["name"]

You can iterate through the dictionary object to discover all names and values.

getAll(): any;

Returns

any

An object with all custom properties in a collection of name/value pairs.

Remarks

[ API set: Mailbox 1.9 ]

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

remove(name)

Removes the specified property from the custom property collection.

To make the removal of the property permanent, you must call the saveAsync method of the CustomProperties object.

remove(name: string): void;

Parameters

name

string

The name of the property to be removed.

Returns

void

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/15-item-custom-properties/load-set-get-save.yaml
var propertyName = $("#propertyName").val();
customProps.remove(propertyName);
console.log(`Custom property "${propertyName}" removed.`);

saveAsync(callback, asyncContext)

Saves item-specific custom properties to the server.

You must call the saveAsync method to persist any changes made with the set method or the remove method of the CustomProperties object. The saving action is asynchronous.

It's a good practice to have your callback function check for and handle errors from saveAsync. In particular, a read add-in can be activated while the user is in a connected state in a read form, and subsequently the user becomes disconnected. If the add-in calls saveAsync while in the disconnected state, saveAsync would return an error. Your callback method should handle this error accordingly.

saveAsync(callback: (asyncResult: Office.AsyncResult<void>) => void, asyncContext?: any): void;

Parameters

callback

(asyncResult: Office.AsyncResult<void>) => void

When the method completes, the function passed in the callback parameter is called with a single parameter of type Office.AsyncResult.

asyncContext

any

Optional. Any state data that is passed to the callback method.

Returns

void

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

Examples

// The following JavaScript code sample shows how to asynchronously use
// the loadCustomPropertiesAsync method to load custom properties that
// are specific to the current item, and the saveAsync method to save
// these properties back to the server. After loading the custom properties,
// the code sample uses the get method to read the custom property myProp,
// the set method to write the custom property myProp, and then finally
// calls the saveAsync method to save the custom properties.

// The initialize function is required for all add-ins.
Office.initialize = function () {
    // Checks for the DOM to load using the jQuery ready function.
    $(document).ready(function () {
        // After the DOM is loaded, add-in-specific code can run.
        var item = Office.context.mailbox.item;
        item.loadCustomPropertiesAsync(customPropsCallback);
    });
};

function customPropsCallback(asyncResult) {
    var customProps = asyncResult.value;
    var myProp = customProps.get("myProp");
    console.log("myProp: " + myProp); // First run on current item will return `undefined`.

    // Set myProp custom property.
    customProps.set("myProp", "value");
    customProps.saveAsync(saveCallback);
}

function saveCallback(asyncResult) {
    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
        console.error(asyncResult.error.message);
    }
    else {
        // Async call to save custom properties completed.
        // Proceed to do the appropriate for your add-in.
    }
}

saveAsync(asyncContext)

Saves item-specific custom properties to the server.

You must call the saveAsync method to persist any changes made with the set method or the remove method of the CustomProperties object. The saving action is asynchronous.

It's a good practice to have your callback function check for and handle errors from saveAsync. In particular, a read add-in can be activated while the user is in a connected state in a read form, and subsequently the user becomes disconnected. If the add-in calls saveAsync while in the disconnected state, saveAsync would return an error. Your callback method should handle this error accordingly.

saveAsync(asyncContext?: any): void;

Parameters

asyncContext

any

Optional. Any state data that is passed to the callback method.

Returns

void

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

set(name, value)

Sets the specified property to the specified value.

The set method sets the specified property to the specified value. You must use the saveAsync method to save the property to the server.

The set method creates a new property if the specified property does not already exist; otherwise, the existing value is replaced with the new value. The value parameter can be of any type; however, it is always passed to the server as a string.

set(name: string, value: string): void;

Parameters

name

string

The name of the property to be set.

value

string

The value of the property to be set.

Returns

void

Remarks

Minimum permission level: ReadItem

Applicable Outlook mode: Compose or Read

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/15-item-custom-properties/load-set-get-save.yaml
var propertyName = $("#propertyName").val();
var propertyValue = $("#propertyValue").val();
customProps.set(propertyName, propertyValue);
console.log(`Custom property "${propertyName}" set to value "${propertyValue}".`);