Hello everyone, I have written code in C# to create and delete appointments in a user created calendar in Outlook, I can delete the appointment on the Outlook's default calendar but not when the appointment is on a user created calendar.
I used a technique I found in https://stackoverflow.com/questions/70306182/get-outlook-appointmentitem-using-globalappointmentid It consists in adding a custom property to the apointment's usersProperties collection, (let's say GAID is the custom property name), then set the value of that property to the appointment GlobalAppointmentID, then you can use that value to find the appointment. For reference below is the pseudocode:
Create Appointment pseudocode:
Outlook.MAPIFolder userCalendar = GetCalendar(calendar);
Outlook.AppointmentItem newAppointment = (Outlook.AppointmentItem)outlookApp.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = startDate;
newAppointment.End = endDate;
newAppointment.Location = location;
newAppointment.Body = "";
newAppointment.Categories = location;
newAppointment.AllDayEvent = false;
newAppointment.Subject = subject;
AddGAID(newAppointment); //Add custom property
newAppointment.Move(userCalendar); //move appointment to user calendar
newAppointment.Save();
And this is the pseudocode I use find and delete the appointment.
Delete appointment pseudocode:
MAPIFolder calendarFolder = GetCalendar(userCalendar);
string filter = String.Format("[GAID] = '{0}'", globalAppointmentID);
Items calendarItems = calendarFolder.Items;
calendarItems.IncludeRecurrences = true;
try
{
Outlook.AppointmentItem appointmentItem = null;
appointmentItem = calendarItems.Find(filter);
if (appointmentItem != null)
{
appointmentItem.Delete();
}
}
catch (System.Exception ex)
{
throw;
}
This works well when the appointment is in the default Outlook calendar, but it doesn't when the appointment is on a user created folder. it gives me this error:

If I loop through the appointments in the user created calendar, I can read the userProperties with the code below and I can confirm that the custom property is there:
foreach (AppointmentItem item in calendarItems)
{
for (int i = 1; i <= item.UserProperties.Count; i++)
{
Console.WriteLine(item.UserProperties[i].Name);
Console.WriteLine(item.UserProperties[i].Value);
}
}
What am I missing?