javascriptoutlookoutlook-addinoffice-jsjavascript-api-for-office

Outlook AddIn GetAsync successful but returns nothing


I've got an Outlook Add In that was developed using the Office Javascript API. It looks at the new email being composed & does things based on who it's going to: https://learn.microsoft.com/en-us/office/dev/add-ins/reference/objectmodel/requirement-set-1.3/office.context.mailbox.item


var resultObjects;
var resultObjects2;
var strMessages = '';
var strTo = '';

var mailbox;
var mailitem;

(function () {
    "use strict";

    // The Office initialize function must be run each time a new page is loaded.
    Office.initialize = function (reason) {
        $(document).ready(function () {

            mailbox = Office.context.mailbox;
            mailitem = mailbox.item;

            mailitem.to.getAsync(function (result) {
                if (result.status === 'failed') {
                    strMessages = 'FAILED';
                } else {
                    strMessages = 'SUCCESS';
                    strTo = result.value[0];
                    resultObjects = result;
                    resultObjects2 = result.value;
                }
            });

            loadApp();
        });
    };
})();

Here are the values of the variables, when the app is loaded & debugger is not running

enter image description here


EDIT


If you 'select' the TO email so that it is bolded... the code works correctly. If you leave the typed-in-text field without selecting the suggested email, it does not work. The same behavior is true for both the Outlook Web Application (@ https://outlook.office.com) and the desktop outlook application.

Does not work enter image description here

Does Work enter image description here


Solution

  • The Office.context.mailbox.item.to.getAsync API will only return resolved recipients. If the TO email address is not resolved (as in the first screenshot titled "Does not Work"), then API will not return the email address until it is resolved (in both desktop and OWA).

    You can use the RecipientsChanged Event, to get newly resolved recipients after you have queried for to.getAsync. This event would fire when a recipient is newly resolved.