【问题标题】:Getting calendar appointments from multiple accounts从多个帐户获取日历约会
【发布时间】:2019-08-21 23:43:08
【问题描述】:

我目前正在创建一个 Outlook 加载项,我正在使用以下功能从我在 Outlook 中连接的所有帐户中获取所有日历约会。

private List<Outlook.MAPIFolder> _calendarsFolder = new List<Outlook.MAPIFolder>();
private List<Outlook.Items> _calendarsItems = new List<Outlook.Items>();

private void GetCalendarsItems(object sender, EventArgs e)
{
    Outlook.NameSpace session = Globals.ThisAddIn.Application.Session;
    Outlook.Accounts accounts = session.Accounts;

    foreach (Outlook.Account account in accounts)
    {
        Outlook.Recipient recipient = session.CreateRecipient(account.DisplayName);
        Outlook.MAPIFolder calendar = null;

        try
        {
            calendar = session.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderCalendar);
        }
        catch (Exception)
        {
            continue;
        }

        if (calendar != null)
        {
            var calendarItems = calendar.Items;
            AttachEvents(calendarItems);

            _calendarsFolder.Add(calendar);
            _calendarsItems.Add(calendarItems);

            foreach (Outlook.AppointmentItem item in calendarItems)
            {
                if (Select_T_OutlookEvent_Exists(item.GlobalAppointmentID)) continue;

                var outlookEvent = new T_OutlookEvent()
                {
                    Id = item.GlobalAppointmentID,
                    Title = item.Subject,
                    Description = item.Body,
                    Location = item.Location,
                    Organizer = item.GetOrganizer().Name
                };

                Insert_T_OutlookEvent(outlookEvent);
            }
        }
    }
}

问题是我只从一个帐户(连接到Exchange,它也是主帐户)获取约会。对于所有其他帐户,GetSharedDefaultFolder 函数调用引发异常(COMException:操作因注册表或安装问题而失败。重新启动 Outlook 并重试。如果问题仍然存在,请重新安装):一个连接到 Exchange 的帐户,其他两个不是。

提前感谢您的帮助!

【问题讨论】:

  • 哪一行代码抛出了 COMException 错误?另外,为什么要使用 GetSharedDefaultFolder?这适用于委派场景。相反,遍历 NameSpace.Stores 并使用 Store.GetDefaultFolder
  • 调用 GetSharedDefaultFolder 时出现异常。我不明白 GetSharedDefaultFolder 是用于委托场景的,我将尝试使用 Stores,非常感谢 Eric!
  • 不客气 - 我已添加我的评论作为答案

标签: .net exchange-server outlook-addin


【解决方案1】:

GetSharedDefaultFolder 用于委派方案,不会让您访问所有本地/个人日历。相反,循环访问 NameSpace.Stores 并使用 Store.GetDefaultFolder 获取每个配置的帐户/商店的日历。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多