exchangewebservices - The enduring saga of when to use EWS vs the Rest API in an Outlook Add-in -
this question based on previous question asked bit more detail , experience since.
first background, have outlook addin should forward users message , move message specific folder. needs work in both owa, , outlook 2016 environments on prem exchange. should work in both of former clients, outlook mobile app o365 users.
my specific question comes down detecting when use ews vs rest api (or ms graph api).
here snippet of of code:
office.initialize = function() { $(document).ready(function() { $('#forward').click(forwardmessage); }); }; function forwardmessage() { if(office.context.mailbox.resturl) { // problem child forwardews(); // works charm } else { forwardrest(); // works fine o365 users } } function forwardrest() { var resthost = office.context.mailbox.resturl; var restid = getitemrestid(); office.context.mailbox.getcallbacktokenasync({isrest: true}, function(result){ if (result.status === "succeeded") { var accesstoken = result.value; $.ajax({ url: resthost + '/v2.0/me/messages/' + restid + '/forward', type: 'post', headers: {'authorization': 'bearer ' + accesstoken}, contenttype: 'application/json', data: json.stringify({ 'torecipients': [ {'emailaddress': { 'address': 'user@outlook.com' }} ] }) }).done(function (response) { sidepane_status('success', 'message forwarded.'); moverest(resthost, accesstoken, restid); }).fail(function(err) { sidepane_status('error', 'could not forward message'); }); } else { sidepane_status('error', 'could not token'); } }); } function moverest(resthost, accesstoken, restid) { var folder = $('#ews_folder').val(); $.ajax({ url: resthost + '/v2.0/me/messages/' + restid + '/move', type: 'post', headers: {'authorization': 'bearer ' + accesstoken}, contenttype: 'application/json', data: json.stringify({ 'destinationid': folder }) }).fail(function(err) { sidepane_status('error', 'could not move message ' + folder); }); } function getitemrestid() { if (office.context.mailbox.diagnostics.hostname === 'outlookios') { return office.context.mailbox.item.itemid; } else { return office.context.mailbox.converttorestid( office.context.mailbox.item.itemid, office.mailboxenums.restversion.v2_0 ); } }
in previous question, told check return of office.context.mailbox.resturl
. if returned url rest api used , if not use ews. issue here on prem exchange user, in owa office.context.mailbox.resturl
returns nothing, great use ews. on outlook 2016 office.context.mailbox.resturl
returns https://exch1.mailhost.com/api
. leads issues.
the first being in case of when on prem exchange user in outlook 2016 returning rest url. following article interacting rest api of officejs, not possible an on prem user access token through officejs office.context.mailbox.getcallbacktokenasync({isrest: true}, function(result){ ... });
, in turn rest api cannot used then.
i would, comment in original question suggested, use ews. however, brings problem mobile. article, ews not supported in mobile, , rest must used. understand on prem user not able use outlook mobile app tho.
the problem arises cannot find way determine api use , in context. if key off of office.context.mailbox.resturl
both on prem , o365 users in owa correctly use respective api's , unicorns , rainbows. however, on prem user in outlook 2016, attempt use rest api when should using ews. , finally, if solely rely on ews addin not work o365 users in outlook mobile clients.
i'm kinda @ loss after month of trial, error, , swimming in sea known microsoft documentation. appreciated
yes, known issue resturl
, docs:
note: outlook clients connected on-premises installations of exchange 2016 custom rest url configured return invalid value
resturl
.
i understand makes bit more complicated. in current state of affairs, here's how make call:
- first check
resturl
. if undefined, ews way go. - if
resturl
has value, trygetcallbacktokenasync
isrest: true
. if doesn't token, fallback ews.
i save setting in user's mailbox (via office.context.roamingsettings
) on future runs add-in skip "discovery" phase , use right api start.
on self-serving side note, i'm working on ongoing side project revamp outlook add-in docs @ https://docs.microsoft.com/en-us/outlook/add-ins/, whole "swimming in sea" feeling. i'd love feedback on casting adrift though, if contact me on twitter appreciated.
Comments
Post a Comment