You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Outlook加载项中存储与检索特定数据?

Outlook加载项持久化用户输入方案

你的需求可以通过自定义属性或Office.js内置的漫游设置实现,以下是具体方案:

一、核心方案:用Graph API(替代EWS/旧REST)操作邮箱级自定义属性

这是微软当前推荐的方案,适合存储全局用户信息(所有加载项会话均可读取)。

1. 存储用户输入

// 获取用户输入的文本
const userInput = document.getElementById('yourInputId').value;

// 获取Graph API访问令牌
Office.context.mailbox.getCallbackTokenAsync({isRest: true}, async (tokenResult) => {
  const accessToken = tokenResult.value;
  const graphEndpoint = 'https://graph.microsoft.com/v1.0/me/mailboxSettings';

  // 调用Graph API设置自定义属性
  await fetch(graphEndpoint, {
    method: 'PATCH',
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      "singleValueExtendedProperties": [
        {
          "id": "String {00020329-0000-0000-C000-000000000046} Name UserCustomData",
          "value": userInput
        }
      ]
    })
  });
});

2. 加载时读取存储的信息

Office.context.mailbox.getCallbackTokenAsync({isRest: true}, async (tokenResult) => {
  const accessToken = tokenResult.value;
  const graphEndpoint = `https://graph.microsoft.com/v1.0/me/mailboxSettings?$select=singleValueExtendedProperties&$expand=singleValueExtendedProperties($filter=id eq 'String {00020329-0000-0000-C000-000000000046} Name UserCustomData')`;

  const response = await fetch(graphEndpoint, {
    headers: { 'Authorization': `Bearer ${accessToken}` }
  });
  const data = await response.json();

  // 将存储的值填充到文本框
  if (data.singleValueExtendedProperties?.length > 0) {
    document.getElementById('yourInputId').value = data.singleValueExtendedProperties[0].value;
  }
});

配置要求

  • 在加载项的manifest.xml中添加权限:<Permissions>MailboxSettings.ReadWrite</Permissions>
  • 属性ID格式固定:String {00020329-0000-0000-C000-000000000046} Name [自定义属性名],可替换UserCustomData为你需要的名称

二、简易替代方案:RoamingSettings

如果仅需存储轻量偏好(≤32KB),无需调用外部API,直接用Office.js内置的漫游设置:

存储

const settings = Office.context.roamingSettings;
settings.set('userCustomInput', userInput);
settings.saveAsync((saveResult) => {
  if (saveResult.status === Office.AsyncResultStatus.Succeeded) {
    // 存储完成
  }
});

读取

const settings = Office.context.roamingSettings;
const storedValue = settings.get('userCustomInput');
if (storedValue) {
  document.getElementById('yourInputId').value = storedValue;
}

三、补充说明

  • 如果需要和特定邮件绑定的信息,可将上述方案中的邮箱级属性替换为邮件项目的扩展属性(Graph API中针对messages资源操作)
  • 旧EWS方案逻辑类似,但微软已逐步停止EWS的新功能开发,优先推荐Graph API

内容的提问来源于stack exchange,提问作者Pearl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:55:25