Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Persist metadata for the same Outlook item in a mailbox using custom properties

Office Add-ins

See sample code that stores data specific to an Outlook item in the user’s mailbox on, and accesses the data subsequently on the supported Outlook hosts and form factors.

Last modified: May 01, 2015

Applies to: apps for Office | Office Add-ins | Outlook

Learn more about supported hosts and other requirements.

Note Note

The name "apps for Office" is changing to "Office Add-ins". During the transition, the documentation and the UI of some Office host applications and Visual Studio tools might still use the term "apps for Office". For details, see New name for apps for Office and SharePoint.

In this article
Loading and getting custom properties
Creating or setting a custom property
Removing a custom property
Additional resources

In Outlook, a mail app can use the CustomProperties object to save data that is specific to an item in the user’s mailbox. This data is accessible in subsequent Outlook sessions by only that app. The data is stored on the Exchange Server for that mailbox, and is accessible in subsequent Outlook sessions on all the form factors that the app supports.

A mail app typically loads custom properties of the current item in the Office.initialize event handler. The following JavaScript code example shows how to load and get custom properties. The example calls the Item.loadCustomPropertiesAsync method to load custom properties of the current item, and passes the callback method myLoadAllCustomPropertiesCallback. Outlook calls the callback method after loading all the custom properties for the item. The myLoadAllCustomPropertiesCallback method gets a custom property. You can further customize the callback method to use the custom properties for the current item.

//Global variables for sample code in this topic.
var _mailbox;
var _Item;
var _CustomProps;

// The initialize function is required for all apps.
Office.initialize = function (reason) {
    // Initialize instance variables to access API objects.
    _mailbox = Office.context.mailbox;
    _Item = _mailbox.item;

    // Checks for the DOM to load using the jQuery ready function.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.

function onInitializeComplete()
    // Load custom properties for the current item.

// Obtains existing custom properties for the current item.
// The custom properties are passed in an object parameter to this callback method.
// Uses the CustomProperties.get method to get the value of the 
// "custom_property_name" property by name.

function myLoadAllCustomPropertiesCallback(obj)
     if (asyncResult.status == Office.AsyncResultStatus.Failed) {
        // Handle the failure.
    else {
        _CustomProps = obj.value;

        if (_CustomProps.get("custom_property_name")) {
            var propValue = _CustomProps.get("custom_property_name");
            // Do something with propValue.

        // Do something with other custom properties in _CustomProps.

Continuing with the preceding example, the following JavaScript function, setCustomProperty, shows how to use the CustomProperties.set method to set a custom property named custom_property_name with the value value, and persist the data by using the CustomProperties.saveAsync method to save all the custom properties for the item back to the Exchange Server. The set method creates the property if the property does not already exist, and assigns the property to the specified value. The saveAsync method saves properties asynchronously. This code sample passes a callback method, saveMyCustomPropertiesCallback, to saveAsync. When the asynchronous call finishes, saveMyCustomPropertiesCallback is called by using one parameter, asyncResult. This parameter is an AsyncResult object that contains the result of and any details about the asynchronous call. You can use the optional userContext parameter to pass any state information from the asynchronous call to the callback function.

// Sets a custom property on an item and persists its value by 
// using saveAsync and passing a callback method saveMyCustomPropertiesCallback.
// Property name is "custom_property_name", property value is "value".
function setCustomProperty()
    _CustomProps.set("custom_property_name", "value");

// Callback method after saving custom properties.
function saveMyCustomPropertiesCallback(asyncResult)
    if (asyncResult.status == Office.AsyncResultStatus.Failed) {
        // Handle the failure.
    // Do something here.

Also extending the preceding examples, the following JavaScript function, removeCustomProperty, shows how to use the CustomProperties.remove method to remove the custom_property_name property and save all the custom properties for the current item back to the Exchange Server.

// Removes a custom property from an item.
// Removes the custom property named custom_property_name and then save.
function removeCustomProperty()
© 2015 Microsoft