使用Office.context.mailbox.item.dateTimeCreated返回undefined问题求助
解决Office.context.mailbox.item.dateTimeCreated返回undefined的问题
核心原因排查
dateTimeCreated属性仅在邮件、会议请求、会议响应类项目中可用,若当前操作的是任务、联系人等非邮件类项目,该属性本身不存在,会直接返回undefined。- 该属性在Office.js 1.1及以上版本支持,但必须确保邮件项目已完全加载,直接在脚本初始化阶段调用会因item对象未就绪导致返回undefined。
分步解决步骤
在就绪回调中异步加载属性
必须等待Office初始化完成且邮件项目加载就绪,通过loadAsync显式加载目标属性后再访问:Office.initialize = function(reason) { $(document).ready(function() { Office.context.mailbox.item.loadAsync(['dateTimeCreated'], function(result) { if (result.status === Office.AsyncResultStatus.Succeeded) { console.log(Office.context.mailbox.item.dateTimeCreated); } else { console.error('加载属性失败: ' + result.error.message); } }); }); };验证当前项目类型兼容性
先确认操作的项目类型是否支持该属性:const itemType = Office.context.mailbox.item.itemType; // 仅邮件、会议请求类项目支持dateTimeCreated if (itemType === Office.MailboxEnums.ItemType.Message || itemType === Office.MailboxEnums.ItemType.MeetingRequest) { // 执行属性获取逻辑 } else { console.error('当前项目类型不支持dateTimeCreated属性'); }确认Office.js版本配置一致
- 检查HTML中的script标签,确保引用的CDN版本与CLI切换的版本匹配,比如1.3版本的引用应为:
<script src="https://appsforoffice.microsoft.com/lib/1.3/hosted/office.js" type="text/javascript"></script> - 清除浏览器缓存,避免旧版本Office.js缓存导致的版本不匹配问题。
- 检查HTML中的script标签,确保引用的CDN版本与CLI切换的版本匹配,比如1.3版本的引用应为:
检查权限配置
在manifest.xml中确保已声明至少ReadItem权限:<Permissions>ReadItem</Permissions>
内容的提问来源于stack exchange,提问作者Rinku Gangishetty
相关产品推荐
相关产品推荐

