none
How to delete an appointment on someone else outlook calendar and sending a cancellation notice in c# RRS feed

  • Question

  • I'm building a interview scheduler application that creates automatic appointments both in the senders outlook calendar and in the receivers.
    This is the code I used to create an appointment.

      private void AddAppointment(Appointment NewAppointment, string MailTo)
            {
                Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); // creates new outlook app
                Microsoft.Office.Interop.Outlook.AppointmentItem oAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); // creates a new appointment
                oAppointment.Subject = NewAppointment.Subject; // set the subject
                oAppointment.Body = NewAppointment.Description; // set the body
                oAppointment.Location = NewAppointment.Location; // set the location
                oAppointment.Start = NewAppointment.Start; // Set the start date 
                oAppointment.End = NewAppointment.End; // End date 
                oAppointment.ReminderSet = true; // Set the reminder
                if (NewAppointment.HasReminder)
                    oAppointment.ReminderMinutesBeforeStart = Convert.ToInt32(NewAppointment.Reminder);
                else
                    oAppointment.ReminderMinutesBeforeStart = 60;
                // reminder time
                oAppointment.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; // appointment importance
                oAppointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
                oAppointment.Save();
                Microsoft.Office.Interop.Outlook.MailItem mailItem = oAppointment.ForwardAsVcal();
                mailItem.To = "mailto: " + MailTo;
                mailItem.Send();
            }


    But now I need a piece of code that can also delete it in the receiver outlook calendar. So far I have this piece of code.

    private void CancellAppointment(string AppointmentID)
            {
                Microsoft.Office.Interop.Outlook.Application OlApp = new Microsoft.Office.Interop.Outlook.Application();
                NameSpace OlNamspace = OlApp.GetNamespace("MAPI");
                MAPIFolder AppointmentFolder = OlNamspace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
                AppointmentFolder.Items.IncludeRecurrences = true;
                foreach (AppointmentItem app in AppointmentFolder.Items)
                {
                    if (app.EntryID == AppointmentID)
                    {
                        app.Delete();
                    }
                } 

            }

    this deletes the appointment in my outlook but I can't figure out how to delete it in the receiver's also. And I also need to send a cancellation notice both to myself and to the other person.   

    • Moved by Weiwei Cai Tuesday, June 14, 2016 3:13 AM not VS IDE issue
    Sunday, June 12, 2016 2:01 PM

Answers

  • Hello Toxy,

    You can delete an appointment item from folders you see in Outlook (or shared ones). In that case you can use the GetDefaultFolder method of the Store class which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the  NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile. 

    Or you can iterate over all folders in Outlook and check their default item type (see the DefaultItemType property of the MAPIFolder class) which returns a constant from the OlItemType enumeration indicating the default Outlook item type contained in the folder. 

    But you can't remove an appointment from a remote calendar (except using Office 365 APIs).


    [custom.development]

    Tuesday, June 14, 2016 12:23 PM
  • Hi Mr.Toxy,

    >> this deletes the appointment in my outlook but I can't figure out how to delete it in the receiver's also. And I also need to send a cancellation notice both to myself and to the other person.   

    I agree with Eugene, you could not delete an appointment from the receiver calendar by Outlook object model. You could delete an appointment and send a cancellation notice to others, but you would not get any notice, if you need, you need to custom it in your code by sending a notice email.

    For canceling appointment with notice, here is a simple code in VBA, you could modify it as your requirement.

    Sub CancelAppointment()
        Dim oAppointment As Outlook.AppointmentItem
        Dim oNameSpace As Outlook.NameSpace
        Dim oFolder As Outlook.Folder
        Set oNameSpace = Application.GetNamespace("MAPI")
        Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
        For Each oAppointment In oFolder.Items
            If oAppointment.Subject = "Test" Then
                oAppointment.MeetingStatus = olMeetingCanceled
                oAppointment.Save
                oAppointment.Send
                oAppointment.Delete
            End If
        Next oAppointment
        Debug.Print "ok"
    End Sub

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 15, 2016 2:52 AM

All replies

  • Hi Mr.Toxy,

    This forum is discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

    Since your problem is more related to outlook development, I help you move this thread to Outlook forum for a better help. Thanks for your understanding.

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 14, 2016 3:12 AM
  • Hello Toxy,

    You can delete an appointment item from folders you see in Outlook (or shared ones). In that case you can use the GetDefaultFolder method of the Store class which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the  NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile. 

    Or you can iterate over all folders in Outlook and check their default item type (see the DefaultItemType property of the MAPIFolder class) which returns a constant from the OlItemType enumeration indicating the default Outlook item type contained in the folder. 

    But you can't remove an appointment from a remote calendar (except using Office 365 APIs).


    [custom.development]

    Tuesday, June 14, 2016 12:23 PM
  • Hi Mr.Toxy,

    >> this deletes the appointment in my outlook but I can't figure out how to delete it in the receiver's also. And I also need to send a cancellation notice both to myself and to the other person.   

    I agree with Eugene, you could not delete an appointment from the receiver calendar by Outlook object model. You could delete an appointment and send a cancellation notice to others, but you would not get any notice, if you need, you need to custom it in your code by sending a notice email.

    For canceling appointment with notice, here is a simple code in VBA, you could modify it as your requirement.

    Sub CancelAppointment()
        Dim oAppointment As Outlook.AppointmentItem
        Dim oNameSpace As Outlook.NameSpace
        Dim oFolder As Outlook.Folder
        Set oNameSpace = Application.GetNamespace("MAPI")
        Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
        For Each oAppointment In oFolder.Items
            If oAppointment.Subject = "Test" Then
                oAppointment.MeetingStatus = olMeetingCanceled
                oAppointment.Save
                oAppointment.Send
                oAppointment.Delete
            End If
        Next oAppointment
        Debug.Print "ok"
    End Sub

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 15, 2016 2:52 AM
  • Hi Edward,

    This has made my application a success, its been a week since i have been looking for a code that works.

    Thank a lot.

    Wednesday, November 15, 2017 8:29 AM