I have a list of emails each corresponding to a shared calendar.
In the AppointmentFound function given an AppointmentItem EntryID I would like to search if the item exists in the shared calendar. The error I get when I run AppointmentFound() is System.Runtime.InteropServices.COMException. ErrorCode -2147467259
Searching for this error code I came across another article developed in the second function (commented) AppointmentFound, but without success. Running out of memory looping through mail items
What is the best way to search for an AppointmentItem for EntryID with the 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;
//}
}
My solution with 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;
}