Programmatically delete calendar meeting in outlook non-default calendar.

Luis 156 Reputation points
2024-01-07T15:54:05.6033333+00:00

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:

errors

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?

Outlook
Outlook
A family of Microsoft email and calendar products.
4,502 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,545 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,366 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luis 156 Reputation points
    2024-01-09T00:30:54.9233333+00:00

    The issue is/was that it requires the custom userProperty to be added to the folder also. I added this line to the code above that creates the appointment:

    userCalendar.UserDefinedProperties.Add("GAID", OlUserPropertyType.olText);
    
    

    See more details in this link https://stackoverflow.com/questions/77770944/how-to-delete-calendar-meeting-in-outlook-non-default-calendar/77771162#77771162

    Hope this helps someone!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.