netOffice Outlook AppointmentItem 按共享文件夹中的 EntryID 筛选

netOffice Outlook AppointmentItem filter by EntryID from a shared folder

我有一个电子邮件列表,每个电子邮件对应一个共享日历。

在给定 AppointmentItem EntryID 的 AppointmentFound 函数中,我想搜索该项目是否存在于共享日历中。 我在 运行 AppointmentFound() 时得到的错误是 System.Runtime.InteropServices.COMException。错误代码 -2147467259

搜索此错误代码时,我发现了另一篇在第二个函数(已评论)AppointmentFound 中开发的文章,但没有成功。

使用 netOffice API 搜索 EntryID 的 AppointmentItem 的最佳方法是什么?

public class TestCalendar {
    private Outlook oOutlook;
    public AppointmentItem AppointmentItem { get; set; }
    private List<string> Owner = new List<string>();
    private DateTime dtFrom;
    private DateTime dtTo;

    public TestCalendar() {
       Inizialize();
       CheckIfExists();
    }

    private void Inizialize() {
       oOutlook = new Outlook();
       dtFrom = new DateTime(DateTime.Now.Year - 1, 01, 01);
       dtTo = new DateTime(DateTime.Now.Year, 12, 31);
    }

    public void CheckIfExists() {
       string entryID;
       int i;
       bool bFound;
       Owner = GetCalendarOwner();
       if(!Owner.Any() && Owner.Count < 1) {
return;
       }
          entryID = "00000000BEF58CC55AC7EC42B5AA253C222DE56707000F9B165872833F4BBFD216F68D0E5C5480000000010D00000F9B035872833F4BBFD896F68D0E5C550000014BBE4A0000";
          foreach(string email in Owner) {
             oOutlook.ProcessSharedFolder(email, dtFrom, dtTo);
             bFound = oOutlook.AppointmentFound(entryID);
             if(!bFound)
                i = SqlFactory.DeleteAppointment(entryID);
          }
    }

    private List<string> GetCalendarOwner() {
       List<string> delegator = new List<string>();

       try {
          delegator.Add("test01@mycompany.com");
          delegator.Add("test02@mycompany.com");
          delegator.Add("test03@mycompany.com");
          delegator.Add("test04@mycompany.com");

       }

       catch(System.Exception ex) {
          throw new System.Exception(Commons.Scope, ex.InnerException);
       }
       return delegator;
    }
 }

 public class TestOutlook {

    public Application oApp;
    private Recipient TeamMember { get; set; }
    public MAPIFolder SharedFolder { get; set; }
    private _NameSpace ns { get; set; }
    private _Items calendarAppointments { get; set; }


    private string restrictCriteria, storeID;


    public _Items ProcessSharedFolder(string email, DateTime from, DateTime to) {
       try {
          TeamMember = oApp.Session.CreateRecipient(email);
          TeamMember.Resolve();
          if(!TeamMember.Resolved) return null;

          SharedFolder = oApp.Session.GetSharedDefaultFolder(TeamMember, OlDefaultFolders.olFolderCalendar);
          storeID = SharedFolder.StoreID;
          ns = oApp.Session;

          if(SharedFolder.DefaultMessageClass != "IPM.Appointment" || TeamMember.DisplayType != 0) {
             throw new System.InvalidOperationException("DefaultMessageClass != IPM.Appointment");
          }
          else {
             calendarAppointments = new _Items();
             restrictCriteria = "[Start]<=\"" + to.ToString("g") + "\"" + " AND [End]>=\"" + from.ToString("g") + "\"";
             calendarAppointments = SharedFolder.Items.Restrict(restrictCriteria);
             if(calendarAppointments == null || !calendarAppointments.Any()) return null;
             return calendarAppointments;
          }
       }
       catch(System.Exception) {
          throw;
       }
    }

    public bool AppointmentFound(string entryID) {
       bool bRes = false;
       try {

          //restrictCriteria = "[EntryId]=\"" + entryID("g") + "\"";

          //calendarAppointments = SharedFolder.Items.Restrict(restrictCriteria);

          AppointmentItem itemFound = (AppointmentItem)ns.GetItemFromID(entryID);

          if(itemFound == null) bRes = false;
          else bRes = true;
       }
       catch(NetOffice.NetOfficeException ex) {

       }
       return bRes;
    }

    //public bool AppointmentFound(string entryID) {
    //   try {
    //      //AppointmentItem item = (AppointmentItem)ns.GetItemFromID(entryID, storeID);

    //      _Items calItems = SharedFolder.Items;
    //      COMObject calItem = null;
    //      do {
    //         if(null == calItem)
    //            calItem = (COMObject)calItems.GetFirst();
    //         if(null == calItem)
    //            break;

    //         // do what you want here

    //         calItem.Dispose();
    //         calItem = (COMObject)calItems.GetNext();
    //      } while(null != calItem);

    //      if(calItem == null) bRes = false;
    //      else bRes = true;
    //   }
    //   catch(NetOffice.NetOfficeException ex) {
    //   }
    //   return bRes;
    //}
 }

我的 LINQ 解决方案。

   public bool AppointmentFound(string entryID) {

         try {
            var query = from AppointmentItem ai in calendarAppointments
                        where ai.EntryID == entryID
                        select ai;
            bRes = query.Any();

         }
         catch(NetOffice.NetOfficeException ex) {
         }
         return bRes;
      }