在Outlook加载项中使用EWS与Rest API的持久传奇

时间:2017-09-14 00:01:15

标签: outlook exchangewebservices office-js outlook-restapi outlook-web-addins

这个问题是基于previous question我提出的问题,但是从那以后有了更多细节和经验。

首先是一些背景,我有一个Outlook Addin应该转发用户消息,然后将消息移动到特定文件夹。这需要在OWA和Outlook 2016环境中适用于Prem Exchange。它既适用于以前的客户端,也适用于O365用户的Outlook Mobile App。

我的具体问题归结为检测何时使用EWS与Rest API(甚至是MS Graph API)。

以下是我的代码片段:

Office.initialize = function() {
    $(document).ready(function() {
        $('#forward').click(forwardMessage);
    });
}; 

function forwardMessage() {
    if(Office.context.mailbox.restUrl) { // The problem child
        forwardEWS(); // Works like a charm
    } else {
        forwardRest(); // Works fine for 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 get 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 to ' + 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
        );
    }
}

在我之前的问题中,我被告知要检查Office.context.mailbox.restUrl的回复。如果它返回了URL,则可以使用Rest API,如果不使用EWS。这里的问题是我的Prem Exchange用户,在OWA中Office.context.mailbox.restUrl没有返回任何内容,那么我只使用EWS。但是,在Outlook 2016 Office.context.mailbox.restUrl上会返回https://exch1.mailhost.com/api。这会导致一些问题。

第一个是Outlook 2016中的On Prem Exchange用户返回Rest URL的情况。在article之后,在OfficeJS的帮助下与Rest API交互,使用On Prem用户无法通过带有Office.context.mailbox.getCallbackTokenAsync({isRest: true}, function(result){ ... });的OfficeJS获取访问令牌,反过来又不能使用Rest API然后使用。

作为我原来的问题建议中的评论,我会使用EWS。然而,这带来了移动设备的另一个问题。从article开始,移动设备不支持EWS,必须使用Rest。我知道一个on Prem用户将无法使用Outlook移动应用程序。

问题出现了,我找不到确定使用什么API以及在什么情况下使用的方法。如果我关闭Office.context.mailbox.restUrl,那么OWA中的On Prem和O365用户将正确使用他们各自的API,并且一切都是独角兽和彩虹。但是,对于Outlook 2016中的On Prem用户,它将尝试在应该使用EWS时使用Rest API。最后,如果我完全依赖EWS,则Addin将不适用于Outlook移动客户端中的O365用户。

经过一个月的审判,错误以及在海上游泳(我称之为Microsoft文档)后,我有点不知所措。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:5)

是的,这是来自docs的<{1}}的已知问题:

  

注意:连接到Exchange 2016的本地安装且配置了自定义REST URL的Outlook客户端将返回restUrl的无效值。

据我所知,这让它变得有点复杂。我会说在目前的情况下,我将如何打电话:

  • 首先检查restUrl。如果未定义,则可以使用EWS。
  • 如果restUrl有值,请尝试使用restUrl getCallbackTokenAsync。如果没有获得令牌,则回退到EWS。

然后我会保存这是用户邮箱中的设置(通过isRest: true),因此在以后的运行中,加载项可以跳过这个&#34;发现&#34;阶段,从一开始就使用正确的API。

在一份自私自利的旁注中,我正在进行一项正在进行的项目,以便在https://docs.microsoft.com/en-us/outlook/add-ins/修改Outlook加载项文档,所以希望这将有助于整个&#34;游泳在海边&#34;感觉。如果你能在Twitter上与我联系,我会非常感激,我很乐意收到关于你的具体内容的反馈意见。

相关问题