Office.js Outlook 加载项 InternetHeaders Prefer ImmutableId [英] Office.js Outlook add-in InternetHeaders Prefer ImmutableId

查看:45
本文介绍了Office.js Outlook 加载项 InternetHeaders Prefer ImmutableId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Outlook 加载项(与 dotnet 核心后端反应).我想利用邮件消息的不可变 ID.这个页面

<块引用>

更新(2019 年 1 月 15 日):此功能现已在 Microsoft Graph v1.0 和 Beta 版中作为选择加入"提供.默认情况下,Graph 将继续返回常规 ID.

因此,为了访问此功能,我需要在 API 请求中发送一个 HTTP 标头:

首选:IdType="ImmutableId"

有一个 API 可以做到这一点:

Office.context.mailbox.item.internetHeaders.setAsync({"Prefer": "IdType=\"ImmutableId\""});

在用于编写消息的 React 组件中,请求在 componentDidMount() 方法中成功,但不生成 ImmutableId [已编辑 - setAsync() 方法仅可用于撰写消息].在编写问题的过程中,我在 此页面:

<块引用>

您需要执行一些额外的步骤才能完成更新.在加载项 HTML 页面的 head 标记中,注释掉或删除任何现有的 office.js 脚本引用,并引用更新后的 JavaScript API for Office 库,如下所示:

<script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>

<块引用>

CDN URL 中 office.js 中的/1/指定使用 Office.js 版本 1 中的最新增量版本.

我已经用更新后的脚本引用更新了 taskpane.html 文件,但请求仍然失败 - id 永远不会改变,即使标头设置成功,itemId 只是当项目在文件夹之间移动时会更改的标准易失性 ID.是否有演示成功使用此功能的示例?

解决方案

第一个文档描述了在调用 Microsoft Graph 或 Exchange REST API 时要设置的 HTML 标头.这对您以后可能很重要,具体取决于您使用 ID 的方式.

要获取 Outlook 项目的不可变 ID:

  1. Office.context.mailbox.item.itemId
  2. 获取 EWS 项目 ID
  3. 进行远程服务调用translateExchangeIds 用于将 REST id 交换为不可变 ID 的 REST API

这是一个关于如何从 Outlook 加载项调用 REST API 的不完整示例.它缺少访问令牌,因为 translateExchangeIds REST API 需要 User.ReadBasic 权限,这不是 getClientAccessToken API 返回的令牌范围.此缩写示例基于 从一篇 Outlook 加载项文章,其中包含有关发出类似请求的更多信息.

var accessToken = ""//需要具有 User.ReadBasic 或更高权限的访问令牌var itemIdFormat = "ewsId";var translateExchangeIdsUrl = Office.context.mailbox.restUrl +'/v2.0/me/translateExchangeIds/';$.ajax({类型:'POST',网址:translateExchangeIdsUrl,数据:JSON.stringify({输入 ID:[Office.context.mailbox.item.itemId],"SourceIdType": itemIdFormat,"TargetIdType": "restImmutableEntryId"}),内容类型:'应用程序/json',数据类型:'json',过程数据:假,标头:{'授权':'承载'+访问令牌}}).完成(功能(结果){控制台日志(结果);}).失败(功能(错误){控制台错误(错误);});

您还可以在 Outlook 加载项上为客户端不可变 ID API 创建功能请求 用户语音.我们在规划过程中会考虑 UserVoice 上的功能请求.

I'm developing an Outlook add-in (React with a dotnet core backend). I want to take advantage of the immutable ID for mail messages. This page says

UPDATE (Jan 15, 2019): This feature is now available in Microsoft Graph v1.0 and beta as an "opt-in". Graph will continue to return regular IDs by default.

So in order to access this feature I need to send an HTTP header in API requests:

Prefer: IdType="ImmutableId"

There is an API that does this:

Office.context.mailbox.item.internetHeaders.setAsync({"Prefer": "IdType=\"ImmutableId\""});

In the React component for composing a message, the request succeeds in the componentDidMount() method but doesn't generate an ImmutableId [Edited - the setAsync() method is only available for composing a message]. In the process of writing the question I found an answer on this page:

You'll need to take a few additional steps to complete the update. In the head tag of your add-in's HTML pages, comment out or delete any existing office.js script references, and reference the updated JavaScript API for Office library as follows:

<script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>

The /1/ in the office.js in the CDN URL specifies to use the latest incremental release within version 1 of Office.js.

I've updated the taskpane.html file with the updated script reference but the request is still failing - the id's are never immutable, even when the header is set successfully, the itemId is just the standard volatile id that changes when the item is moved between folders. Is there a sample that demonstrates the successful use of this functionality?

解决方案

The first doc describes an HTML header to set when making calls to a Microsoft Graph or Exchange REST API. This may be important for you later depending on how you use the id.

To get the immutable id for an Outlook item:

  1. Get the EWS item id from Office.context.mailbox.item.itemId
  2. Make a remote service call to the translateExchangeIds REST API to exchange the REST id for an immutable ID

This is an incomplete example of how to call the REST API from an Outlook add-in. It's missing an access token because the translateExchangeIds REST API requires User.ReadBasic permissions, which isn't a scope of the token returned by the getClientAccessToken API. This abbreviated example is based on the making REST API calls from an Outlook add-in article, which contains more information about making other requests like this one.

var accessToken = "" // Need access token with User.ReadBasic or greater permissions
var itemIdFormat = "ewsId";

var translateExchangeIdsUrl = Office.context.mailbox.restUrl +
    '/v2.0/me/translateExchangeIds/';

$.ajax({
    type: 'POST',
    url: translateExchangeIdsUrl,
    data: JSON.stringify({
        InputIds : [
            Office.context.mailbox.item.itemId
        ],
        "SourceIdType": itemIdFormat,
        "TargetIdType": "restImmutableEntryId"
    }),
    contentType: 'application/json',
    dataType: 'json',
    processData: false,
    headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function(result){
    console.log(result);
}).fail(function(error){
    console.error(error);
});

You also can create a feature request for a client-side immutable id API on the Outlook add-ins UserVoice. Feature requests on UserVoice are considered when we go through our planning process.

这篇关于Office.js Outlook 加载项 InternetHeaders Prefer ImmutableId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆