如何使用 MS Graph 在 Outlook 中读取 "UserProperty" 值

How do I read the "UserProperty" value in Outlook using MS Graph

我们有将自定义数据写入 Outlook AppointmentItem 对象的 "UserProperties" 集合的遗留代码。我们现在已切换到使用 Outlook 网页版 (OWA)。

如何使用 MS Graph 检索这些值?

我一直在研究这个文档 (Outlook extended properties overview) but I can't get it to work. I'm using the MS Graph Explorer.

这是我要为其检索信息自定义数据的事件。

{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#users('45d5e17d-348a-4ca8-b53c-c7d353b928b3')/events",
"value": [
    {
        "@odata.etag": "W/\"GKUifH9QgE6zbEa7VG6rswABBwIJDw==\"",
        "id": "AAMkADU4MzkxN2RmLTdiZDAtNDIwYS04NjQzLTUzNzMyMjM0Y2VkNQBGAAAAAABGjw0ByCaySL6aUxJmew3qBwDwiT27qO5xT6RMWiWBhwRzAAAADIqqAADdUihFgnKFTYATejxXFszxADsYsAgxAAA=",
        "createdDateTime": "2018-07-11T19:17:12.340183Z",
        "lastModifiedDateTime": "2018-09-17T19:50:10.7118964Z",

我假设此事件的 "id" 值是我应该使用的值。

这是我正在进行的 REST 调用(注意:使用 BETA)

https://graph.microsoft.com/beta/me/events('AAMkADU4MzkxN2RmLTdiZDAtNDIwYS04NjQzLTUzNzMyMjM0Y2VkNQBGAAAAAABGjw0ByCaySL6aUxJmew3qBwDwiT27qO5xT6RMWiWBhwRzAAAADIqqAADdUihFgnKFTYATejxXFszxADsYsAgxAAA=')?$expand=SingleValueExtendedProperties($filter=id%20eq%20'Integer%20{0006303D-0000-0000-C000-000000000046}%20Name%20TaskID'  )

UserProperty 名称是 "TaskID",它包含一个整数。我不清楚 GUID 值应该是多少。

我试过 AppointmentItem 本身的 GUID;然后是 AppointmentItem 中包含的 "UserProperties" 集合的 GUID,最后是 "UserProperties" 集合中包含的 "UserProperty" 属性 的 GUID。没有任何效果。

有什么线索吗?

创建自定义数据的代码示例

  1. 为 Outlook 创建 VSTO 项目
  2. 复制下面的粘贴代码
  3. 在 Outlook 日历中创建约会
  4. 将主题行更新为 "MS Graph - Extended Properties Test"。
  5. 关闭 Outlook
  6. 编译和运行代码。
  7. 打开您的约会并进行一些(不是主题)并保存。
  8. 加载项会根据您保存的时间更新您的约会正文。
  9. 尝试使用 Microsoft Graph 检索此数据

    using System;
    using System.Runtime.InteropServices;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace AddCustomProperty
    {
        public partial class ThisAddIn
        {
            Outlook.Items _items;
            Outlook.Folder _calendar;
            Outlook.Inspectors _inspectors;
            const string sCustomData = "MyCustomData";
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                _calendar = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
    
               _items = _calendar.Items;
    
               _items.ItemChange += eventChange;
    
               _inspectors = this.Application.Inspectors;
               _inspectors.NewInspector += newInspectorWindow;
    
    
            }
    
            private void newInspectorWindow(Outlook.Inspector Inspector)
            {
                Object oAppointmentItem = null;
                Outlook.UserProperties userProperties = null;
                Outlook.UserProperty userProperty = null;
    
                try
                {
                    oAppointmentItem = Inspector.CurrentItem;
                    if (oAppointmentItem is Outlook.AppointmentItem)
                    {
                        userProperties = ((Outlook.AppointmentItem)oAppointmentItem).UserProperties;
                        userProperty = userProperties.Find(sCustomData);
                        if( userProperty != null)
                        {
                            ((Outlook.AppointmentItem)oAppointmentItem).Body = string.Format("MY CUSTOM DATA FOUND [{0}]: {1}\n", DateTime.Now, userProperty.Value);                    
                        }
                    }
                }
                catch(Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
                finally
                {
                    if (userProperty != null) { Marshal.ReleaseComObject(userProperty); userProperty = null; }
                    if (userProperties != null) { Marshal.ReleaseComObject(userProperties); userProperties = null; }
                    if (oAppointmentItem != null) { Marshal.ReleaseComObject(oAppointmentItem); oAppointmentItem = null; }
                }
            }
    
            private void eventChange(object Item)
            {
                Outlook.AppointmentItem apptItem = null;
                Outlook.UserProperties userProperties = null;
                Outlook.UserProperty userProperty = null;
    
                try
                {
                    apptItem = Item as Outlook.AppointmentItem;
    
                    if (apptItem.Subject == "MS Graph - Extended Properties Test") 
                    {
                        userProperties = apptItem.UserProperties;
                        userProperty = userProperties.Find(sCustomData);
                        if( userProperty == null)
                        {
                            userProperty = userProperties.Add(sCustomData, Outlook.OlUserPropertyType.olInteger);
                            userProperty.Value = 10;
                        }
                        else
                        {
                            ((Outlook.AppointmentItem)apptItem).Body = string.Format("MY CUSTOM DATA FOUND [{0}]: {1}\n", DateTime.Now, userProperty.Value);
    
                        }
    
                    }
                }
                catch( Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
                finally
                {
                    if( userProperty != null) { Marshal.ReleaseComObject(userProperty); userProperty = null; }
                    if (userProperties != null) { Marshal.ReleaseComObject(userProperties); userProperties = null; }
    
                }            
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                // Note: Outlook no longer raises this event. If you have code that 
                //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            #endregion
        }
    

我在另一个网站上收到了我的答案。

问题在于我在一个查询中使用的 GUID。我使用的是 UserProperty 对象的 GUID 而不是继承自 UserProperty

的对象的 GUID

INCORRECT GUID

正确的 GUID 来自 MAPI 对象本身。

CORRECT GUID

最终调用如下所示:Complete Answer

https://graph.microsoft.com/v1.0/me/events('AAMkADU4MzkxN2RmLTdiZDAtNDIwYS04NjQzLTUzNzMyMjM0Y2VkNQBGAAAAAABGjw0ByCaySL6aUxJmew3qBwDwiT27qO5xT6RMWiWBhwRzAAAADIqqAAAYpSJ8f1CATrNsRrtUbquzAAEOAONsAAA=')?$expand=singleValueExtendedProperties($filter=id%20eq%20'Integer%20{00020329-0000-0000-C000-000000000046}%20Name%20MyCustomData')