Delete an Appointment from the Organizers Calendar and the Invitees Calendar

I can't get the Appointment.Delete() method to remove calendar entries from anyones calendar.  I'm using exchange 2010 sp2 and ews.  Here's my code..

publicstaticstringSendAppointmentViaEWS(MailMessagemsgOriginal, stringsummary, System.Net.Mail.Attachment[] attachments, DateTimestart, DateTimeend, stringlocation)

        {

           

stringreturnThis = "SendAppointmentViaEWS started "+ DateTime.Now.ToShortTimeString();

           

ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

           

ExchangeServiceservice = newExchangeService(ExchangeVersion.Exchange2010_SP2);

           

//service.Credentials = new WebCredentials("smithru@chesterfield.gov", "");


           

//service.UseDefaultCredentials = true;


           

stringemailSender = ConfigurationManager.AppSettings["EmailSender"];

           

Debug.WriteLine("emailSender:"+ emailSender);

           

stringwebCredentialUserName = ConfigurationManager.AppSettings["apptSender"];   //"CDISTestCalendar";


            webCredentialUserName =

"PLZoningOpinion";

           

           

//new 3/27/2013 -- retrieve encrypted value from database


           

ConferenceDataAccessdata = newConferenceDataAccess();

           

stringwebCredentialPW = data.RetrieveEncryptedPasswordForEWS(webCredentialUserName);

           

//string webCredentialPW = ConfigurationManager.AppSettings["apptSenderPW"]; // "Z0nion0pinion";


           

           

//end new 3/27/2013 -- retrieve encrypted value from database


            service.Credentials =

newWebCredentials(webCredentialUserName, webCredentialPW, "Chesterfield");

            service.AutodiscoverUrl(

"daill@chesterfield.gov", RedirectionUrlValidationCallback); //TODO: replace with other credentials ?


           

Appointmentappointment = newAppointment(service);

           

EmailMessagemessage = newEmailMessage(service);

           

foreach(System.Net.Mail.Attachmentatt inattachments)

            {

               

varmemoryStream = newMemoryStream();

                att.ContentStream.CopyTo(memoryStream);

               

byte[] theBytes = memoryStream.ToArray();

                appointment.Attachments.AddFileAttachment(att.Name, theBytes);

            }

           

            appointment.Body = summary;

            appointment.Subject = msgOriginal.Subject; 

// + [EWS]


            appointment.Start = start;

            appointment.End = end;

            appointment.Location = location;

           

intaa = 1;

           

//Subject " Pre-Application Conference Cancellation Notification - 12ZO0244  " string


           

intstartOfCaseNumber = msgOriginal.Subject.IndexOf("ZO") - 2; //TODO: make this configurable?


           

intlengthOfCaseNumber = 8; //TODO: make this configurable?


           

stringcaseNumber = msgOriginal.Subject.Substring(startOfCaseNumber, lengthOfCaseNumber).Trim();

           

Debug.WriteLine(caseNumber);

           

if(msgOriginal.To.Count > 0)

            {

               

foreach(MailAddressmaam inmsgOriginal.To.Reverse()) //add in reverse order so that appointment shows up from the right entity


                {

                    appointment.RequiredAttendees.Add(maam.DisplayName, maam.Address);

                   

                   

Debug.WriteLine("adding "+ aa.ToString()  +  ":"+ maam.Address);

                    aa++;

                   

FindItemsResults<Appointment> findResults = service.FindAppointments(

                       

WellKnownFolderName.Calendar,

                       

newCalendarView(appointment.Start, appointment.End));

                                       

List<Item> items = newList<Item>();

                                       

foreach(Appointmenta infindResults)

                                        {

                                            a.Delete(

DeleteMode.HardDelete, SendCancellationsMode.SendToNone);

                                        }

                }

                appointment.Body.BodyType =

BodyType.HTML;               

               

//try to figure out if this is a cancellation


               

if(msgOriginal.Subject.ToUpper().IndexOf("CANCEL") > -1)

                {

                   

Debug.WriteLine("It's a cancellation!");

                   

CalendarViewcalendarView = newCalendarView(start, end);

                   

varitems = service.FindAppointments(WellKnownFolderName.Calendar, calendarView);

                   

foreach(Appointmentapp initems)

                    {

                       

//Debug.WriteLine(app.Subject);


                      

// Debug.WriteLine(app.Start);


                       

if(app.Subject.IndexOf(caseNumber) > 0)

                        {

                           

//appointment.Delete(DeleteMode.MoveToDeletedItems, SendCancellationsMode.SendOnlyToAll);


                           

//app.CancelMeeting();


                            app.Update(

ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);

                            app.Body = appointment.Body.ToString();

                            app.CancelMeeting(app.Body);                           

                            returnThis =

"Appointment deleted.";

                           

returnreturnThis;

                        }

                       

//if (app.Body.Text.Trim() == "Timetable")


                       

//{


                       

//    app.Delete(DeleteMode.HardDelete);


                       

//}


                    }

                   

//appointment.CancelMeeting(msgOriginal.Subject);


                   

//appointment.Save();// (SendCancellationsMode.SendOnlyToAll);


                   

//appointment.CancelMeeting(msgOriginal.Subject);


                   

//--doesn't work --> appointment.Delete(DeleteMode.SoftDelete, SendCancellationsMode.SendToNone);                   


                }

               

               

//TODO: figure out why this method gets called multiple times


               

//if (emailsSent == 0)


               

//{


                    appointment.Save(

SendInvitationsMode.SendOnlyToAll);

                  

                   

                    emailsSent++;

               

//}


            }

           

else


            {

                returnThis =

"no one to send email to";

            }

           

Debug.WriteLine(returnThis);

           

returnreturnThis;

        }

June 6th, 2013 7:48pm

Did you ever get this working? I am working on a similar project, perhaps we could share some ideas/code?
Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 1:16pm

Dan,

or you could just use power shell to delete the meeting from the user(s) mailbox. 

Search-mailbox -searchquery "kind:calendar from:test@contoso.com" -targetmailbox sample@contoso.com -targetfolder "Deleting Meeting" -deletecontent -force


March 24th, 2015 2:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics