In my outlook WebAddin, i am trying to register for mail ItemChange event using below code.
Office.context.mailbox.addHandlerAsync(Office.EventType.ItemChanged, mailItemSelectionChanged, [], function (result) {
if(result && result.status != 'succeeded'){
console.error('result => ' + result);
}
});
Whenever user changes mail in pinned mode, i receive mail change event for first time. then if there is change in conversation, i am reloading the plugin with location.reload() to clear the cache and load addin fresh.
After reload of plugin, it fails to register mailItemChange event and throwing below error :
{"code": 5001, "message": "An internal error has occurred.", "name": "Internal Error"}
It is failing in Browser and some windows machines(working in many other cases).
outlookDiagnostics :
{"host": "Outlook", "platform": "OfficeOnline", "version": "16.0.9215.1000"}
I could figure out the following behaviors with respect to Office.EventType.ItemChanged event registration:
So in your case, you need to unregister the event before calling the location.reload() as shown below.
Office.context.mailbox.removeHandlerAsync(Office.EventType.ItemChanged, {handler: mailItemSelectionChanged}, function(result) {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log('Item Change event could not be unregistered.');
console.log(result.error);
}
else {
console.log('Item Change event unregistered successfully.');
}
});
setTimeout(function() {location.reload();}, 100);
Those who want to navigate to the same or another web-page from their add-in, they can attach click event handlers to the anchor tags (or buttons) in order to ensure that ItemChanged event handlers have been unregistered before the current page is unloaded. I have done this using the following code:
$(document).ready(function() {
$('.NavBarContainer a').toArray().forEach(function(anchor1, index) {
$(anchor1).click(function(event) {
if(itemChangeEventRegistered) {
unregisterItemChangeHandler();
setTimeout(function() {window.location = anchor1.href;}, 100);
return false;
}
return true;
});
});
});