private OutlookTasksWrapper GetOutlookEntriesForSelectedTimeRange() { var disposeOutlookInstances = false; Application application = null; NameSpace nameSpace = null; MAPIFolder defaultOutlookCalendar = null; Items outlookItems = null; var outlookTasks = new List<ReminderTask>(); //Close and Shutdown try { // Get Application and Namespace GetOutlookApplication(out disposeOutlookInstances, out application, out nameSpace, ProfileName); // Get Default Calendar defaultOutlookCalendar = OutlookTaskList != null ? nameSpace.GetFolderFromID(OutlookTaskList.EntryId, OutlookTaskList.StoreId) : nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderTasks); if (OutlookTaskList == null) { OutlookTaskList = new OutlookFolder { Name = defaultOutlookCalendar.Name, EntryId = defaultOutlookCalendar.EntryID, StoreId = defaultOutlookCalendar.StoreID }; } // Get outlook Items outlookItems = defaultOutlookCalendar.Items; if (outlookItems != null) { var items = outlookItems.Cast<TaskItem>(); var taskItems = items as TaskItem[] ?? items.ToArray(); if (taskItems.Any()) { var id = defaultOutlookCalendar.EntryID; foreach (var appointmentItem in taskItems) { try { var app = GetTaskFromItem(id, appointmentItem); outlookTasks.Add(app); } catch (Exception exception) { Logger.Error(exception); } finally { Marshal.FinalReleaseComObject(appointmentItem); } } } } } catch (Exception exception) { Logger.Error(exception); return new OutlookTasksWrapper { Tasks = null, WaitForApplicationQuit = disposeOutlookInstances }; } finally { if (disposeOutlookInstances) { if (nameSpace != null) { nameSpace.Logoff(); } } //Unassign all instances if (outlookItems != null) { Marshal.FinalReleaseComObject(outlookItems); outlookItems = null; } if (defaultOutlookCalendar != null) { Marshal.FinalReleaseComObject(defaultOutlookCalendar); defaultOutlookCalendar = null; } if (nameSpace != null) { Marshal.FinalReleaseComObject(nameSpace); nameSpace = null; } if (disposeOutlookInstances) { // Casting Removes a warninig for Ambigous Call application.Quit(); Marshal.FinalReleaseComObject(application); } application = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return new OutlookTasksWrapper { Tasks = outlookTasks, WaitForApplicationQuit = disposeOutlookInstances }; }
private OutlookAppointmentsWrapper GetOutlookEntriesForSelectedTimeRange(DateTime startDate, DateTime endDate, bool skipPrivateEntries) { var disposeOutlookInstances = false; Application application = null; NameSpace nameSpace = null; MAPIFolder defaultOutlookCalendar = null; Items outlookItems = null; var outlookAppointments = new List<Appointment>(); //Close and Shutdown try { // Get Application and Namespace GetOutlookApplication(out disposeOutlookInstances, out application, out nameSpace, ProfileName); // Get Default Calendar defaultOutlookCalendar = OutlookCalendar != null ? nameSpace.GetFolderFromID(OutlookCalendar.EntryId, OutlookCalendar.StoreId) : nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar); if (OutlookCalendar == null) { OutlookCalendar = new OutlookFolder { Name = defaultOutlookCalendar.Name, EntryId = defaultOutlookCalendar.EntryID, StoreId = defaultOutlookCalendar.StoreID }; } // Get outlook Items outlookItems = defaultOutlookCalendar.Items; if (outlookItems != null) { //Add Filter to outlookItems to limit and Avoid endless loop(appointments without end date) outlookItems.Sort("[Start]", Type.Missing); outlookItems.IncludeRecurrences = true; var min = startDate; var max = endDate; // create Final filter as string var filter = "[Start] >= '" + min.ToString("g") + "' AND [End] <= '" + max.ToString("g") + "'"; Logger.Info("Outlook filter string : "+filter); //Set filter on outlookItems and Loop through to create appointment List var outlookEntries = outlookItems.Restrict(filter); if (outlookEntries != null) { var appts = outlookEntries.Cast<AppointmentItem>(); var appointmentItems = appts as AppointmentItem[] ?? appts.ToArray(); if (appointmentItems.Any()) { var id = defaultOutlookCalendar.EntryID; foreach (var appointmentItem in appointmentItems) { try { if (skipPrivateEntries && (appointmentItem.Sensitivity == OlSensitivity.olPrivate || appointmentItem.Sensitivity == OlSensitivity.olConfidential)) { continue; } var app = GetAppointmentFromItem(id, appointmentItem); outlookAppointments.Add(app); } catch (Exception exception) { Logger.Error(exception); } finally { Marshal.FinalReleaseComObject(appointmentItem); } } } } else { Logger.Warn("Outlook items null, check short date & time format in date time settings of your system."); } } else { Logger.Warn("Outlook items null, check short date & time format in date time settings of your system."); } } catch (Exception exception) { Logger.Error(exception); return new OutlookAppointmentsWrapper { Appointments = null, WaitForApplicationQuit = disposeOutlookInstances }; } finally { if (disposeOutlookInstances) { if (nameSpace != null) { nameSpace.Logoff(); } } //Unassign all instances if (outlookItems != null) { Marshal.FinalReleaseComObject(outlookItems); outlookItems = null; } if (defaultOutlookCalendar != null) { Marshal.FinalReleaseComObject(defaultOutlookCalendar); defaultOutlookCalendar = null; } if (nameSpace != null) { Marshal.FinalReleaseComObject(nameSpace); nameSpace = null; } if (disposeOutlookInstances) { // Casting Removes a warninig for Ambigous Call application.Quit(); Marshal.FinalReleaseComObject(application); } application = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return new OutlookAppointmentsWrapper { Appointments = outlookAppointments, WaitForApplicationQuit = disposeOutlookInstances }; }