在Office.js中,我们如何使用Office.context.mailbox.item.saveAsync响应(克服ErrorItemNotFound)?

In Office.js, How do we use the Office.context.mailbox.item.saveAsync response (overcoming ErrorItemNotFound)?

DOC 说:

Note: If your add-in calls saveAsync on an item in compose mode in order to get an item ID to use with EWS or the REST API, be aware that when Outlook is in cached mode, it may take some time before the item is actually synced to the server. Until the item is synced, using the itemId will return an error.

据我所知,这就是我在尝试使用该 ID 时出现 ErrorItemNotFound 问题的原因? (遗憾的是,Microsoft 没有明确告诉我们 会发生什么 错误)。

由于我的代码是异步调用的 - 我究竟该如何等待提到的“某个时间”?我们是否设置了一个定时器来每隔一秒重试一次?我们什么时候放弃??当项目同步完成时,我还能做些什么来给我回电以继续吗? [仅供参考 - 即使在保存后等待 10 秒对我来说也不起作用]

请注意,我预计我的用户可能正在撰写带有大附件的邮件,因此虽然大多数无附件邮件应该在不到 1 秒的时间内同步,但附加大 pdf/zip/etc 文件的人很容易导致超过 1此处延迟一分钟...

您最好的办法是开始轮询服务器端出现的项目。例如,当您使用子顺序 EWS 查询和循环中从 saveAsync 获得的 Id 并等待成功时,您可能会尝试一个丑陋的解决方案。

例如,我注意到开发人员如何尝试处理此类场景的以下示例:

app.makeEwsRequestAsync = function (request, callback, countRepeatIfCrash, callbackIfCrash) {
        try {
            Office.context.mailbox.makeEwsRequestAsync(request, function (asyncResult) {
                
                try {
                    
                    if (asyncResult.status !== 'succeeded') {
                        app.showError(asyncResult.error.message);
                        return;
                    } else {
                        var $result = app.getResponseElementByName(asyncResult.value, 'm:ResponseCode');
                        
                        if ($result) {
                            var responseCOde = $result.text();
                            if (responseCOde !== 'NoError') {
                                if (countRepeatIfCrash > 0) {
                                    setTimeout(function () {
                                        app.makeEwsRequestAsync(request, callback, countRepeatIfCrash - 1);
                                    }, 500);
                                } else if (callbackIfCrash) {
                                    setTimeout(function() {
                                        callbackIfCrash();
                                    }, 500);
                                } else if (responseCOde === 'ErrorItemNotFound') {
                                    app.showError('EWS ' + responseCOde, function () {
                                        app.makeEwsRequestAsync(request, callback, 70);
                                    });
                                }
                                else {
                                    app.showError('EWS ' + responseCOde);
                                }
                                return;
                            }
                        }
                    }
                    
                    callback(asyncResult);

                } catch (e) {
                    app.showError(e);
                }
            });
        } catch (e) {
            app.showError(e);
        }        
    }

有关详细信息,请参阅 App for Outlook: EWS request failed with item Id returned by item.saveAsync on compose new message

您也可以尝试使用简单的 GetItem 请求:

<GetItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
    <ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
    </ItemShape>
    <ItemIds><t:ItemId Id="' + itemId + '"/></ItemIds>
</GetItem>

如果项目是在交换时创建的,请求应该 return ChangeKey