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.
2 Answers
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).

- 1
- 1

- 722
- 1
- 8
- 19
-
So I basically went with dirty checking like you suggested, and now that I'm looking for where I found it in the API Docs a week or so after I saw it I cannot for the life of me find it. I think it was an embarrassing case of having too many tabs open while reading documentation and imagining things! Either way, this appears to be the only way to accomplish this. – Phil Barresi Jun 15 '15 at 00:56
AFAIK you cannot detect this in "real-time". The Mailbox API has very few events that you can hook into.

- 5,706
- 2
- 22
- 38