фильтр netOffice Outlook AppointmentItem по EntryID из общей папки

У меня есть список электронных писем, каждое из которых соответствует общему календарю.

В функции AppointmentFound с заданным идентификатором записи AppointmentItem я хотел бы найти, существует ли элемент в общем календаре. Ошибка, которую я получаю при запуске AppointmentFound(), — это System.Runtime.InteropServices.COMException. Код ошибки -2147467259

В поисках этого кода ошибки я наткнулся на другую статью, разработанную во второй функции (с комментариями) AppointmentFound, но безуспешно. Нехватка памяти при циклическом просмотре почтовых сообщений

Как лучше всего искать AppointmentItem для EntryID с помощью netOffice API?

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("[email protected]");
          delegator.Add("[email protected]");
          delegator.Add("[email protected]");
          delegator.Add("[email protected]");

       }

       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;
    //}
 }

person undertree    schedule 21.01.2020    source источник


Ответы (1)


Мое решение с 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;
      }
person undertree    schedule 21.01.2020