简体   繁体   中英

How do I watch for changes to the To/CC fields for emails in Outlook Add-In's Javascript API?

In the documentation for older Office API's I'm seeing that there was the ability to watch for changes to the TO/CC fields as the user composes an email. Is this exposed in their new Javascript API for the Office 365? I can't seem to find a definitive answer either way.

This may not be the answer you were hoping for, because I couldn't find any reference to a watcher like you described, but here's my "inelegant, but it works" solution.

In your Add-In, declare a global (to the app) variable called toRecipients . Next, in the Office.initalize function add the following code that initializes the toRecipients variable, then starts a loop to check for changes.

var item = Office.context.mailbox.item;
if (item.itemType === Office.MailboxEnums.ItemType.Message) {
    item.to.getAsync(function(result) {
        toRecipients = result.value;
    }); 
}

setInterval(function(){ isToRecipientsChanged(); }, 1000);

Here's the code that checks for a change. I used an equals function to check if the "To" recipients have changed.

function isToRecipientsChanged() {
    var item = Office.context.mailbox.item;
    item.to.getAsync(function(result) {
        if (!toRecipients.equals(result.value)) {
            toRecipients = result.value;
        }
    });     
}

Finally, here's the equals method I used. I got it from another StackOverflow question . Notice that I changed the check to check the email addresses instead of object instances.

Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length) {
        return false;
    }

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i].address != array[i].address) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}  

So there's my "inelegant, but it works" solution. However, if you provide me with the older documentation you referenced in your question, I'll ask around to find if the new API lets you do this task more efficiently (and more elegantly).

AFAIK you cannot detect this in "real-time". The Mailbox API has very few events that you can hook into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM