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

时间:2018-09-17 22:04:56

标签: office365 microsoft-graph outlook-restapi mapi

我们有旧版代码,可将自定义数据写入Outlook AppointmentItem对象的“ UserProperties”集合。现在,我们已切换为使用Web上的Outlook(OWA)。

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

我一直在浏览本文档(Outlook extended properties overview),但无法正常使用。我正在使用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中包含的“ UserProperty”集合的GUID,最后是“ UserProperty”集合中包含的“ 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
        }
    

1 个答案:

答案 0 :(得分:0)

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

问题在于我在其中一个查询中使用的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')