0

I am developing an Outlook add in and I need access to all the mails in the current mailbox (or even better, all mails, the current user has access to). The use case is quite simple. I have a collection of message ids, which I need to categorize.

I can use Office.context.mailbox to access the mailbox, but I can't find a way to select an element within it. Any ideas on how to do that?

I'd take a server-side solution, too. As described here: Add Outlook Category to mail via MS Graph

greg-e
  • 374
  • 4
  • 18

2 Answers2

0

Outlook web add-ins work under the context of current item only. If you need to access other item I'd suggest using EWS (which is going to be obsolete) or Graph API (recommended now) for that. See Use the Outlook REST APIs from an Outlook add-in for more information about that. Outlook REST APIs are also available through the Microsoft Graph endpoint but there are some key differences, including how your add-in gets an access token. For more information, see Outlook REST API via Microsoft Graph.

If you need to develop a solution for the whole storage you may consider creating a VSTO based add-in for that. Read more about that in the Walkthrough: Create your first VSTO Add-in for Outlook article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Please find below a code sample accessing all the message of an outlook conversation using the REST API. The use of office promises is compatible with IE11 and makes the code more readable than cascading async calls.

The getCallbackTokenAsync call is required. It gets an OAuth token which is then used to authenticate the REST API xhr request against Exchange. Feel free to change the called URL to match your use case.

//https://stackoverflow.com/questions/44377278/get-first-email-from-conversation-id-using-microsoft-graph
//https://graph.microsoft.com/v1.0/me/messages?$filter=(conversationId eq 'yourConversationId')
function getEmailConversationMessagesPromise(conversationId) {
    return new Office.Promise(function(resolve, reject) {
        Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (getCallbackTokenAsyncResult) {
            var token = getCallbackTokenAsyncResult.value;
            var searchMessagesUrl = Office.context.mailbox.restUrl + "/v2.0/me/messages/?$filter=conversationId eq '" + encodeURIComponent(conversationId) +"'";

            var xhr = new XMLHttpRequest();
            xhr.open('GET', searchMessagesUrl);
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Authorization", "Bearer " + token);
            xhr.onload = function (e) {
                messages = JSON.parse(this.response).value;
                resolve(messages);
            }
            xhr.onerror = function () {
                reject("Error when trying to retrieve email conversation messages list");
            }
            xhr.send();
        });
    });
}
kjack51
  • 31
  • 5