/** * Function: CreateNewAppointment * Creates a new appointment. * Returns the AppID (appointment id) for future changes **/ static string CreateNewAppointment(ExchangeService service, string subject, string body, DateTime date, int duration, string status) { Console.WriteLine("Creating a New Item: " + subject); // Create an appointmet and identify the Exchange service. Appointment appointment = new Appointment(service); // Set details appointment.Subject = subject; appointment.Body = body; appointment.Start = date; appointment.End = appointment.Start.AddHours(duration); StringList categories = new Microsoft.Exchange.WebServices.Data.StringList(); categories.Add(status); appointment.Categories = categories; appointment.IsReminderSet = false; // Save and Send appointment.Save(SendInvitationsMode.SendToNone); // Get ItemId for future updates return ""+appointment.Id+""; }
/** * Function: UpdateAppointment * Updates an existing appointment **/ static void UpdateAppointment(ExchangeService service, string appId, string subject, string body, DateTime date, int duration, string status) { Console.WriteLine("Updating Appointment: " + appId); // Bind existing appointment to an identifier Appointment appointment = Appointment.Bind(service, new ItemId(appId)); // Make changes depending on the validity of the inputs if ((subject != null)&&(subject != "")) { appointment.Subject = subject; } if ((body != null)&&(body != "")) { appointment.Body = body; } if (date != DateTime.Parse("01/01/12 00:00:00")) { // Gets the initial duration to perserve it, if duration also not changed TimeSpan initialDuration = appointment.Duration; appointment.Start = date; appointment.End = appointment.Start.Add(initialDuration); } if (duration > 0) { appointment.End = appointment.Start.AddHours(duration); } if ((status != null)&&(status != "")) { StringList categories = new Microsoft.Exchange.WebServices.Data.StringList(); categories.Add(status); appointment.Categories = categories; } // Save changes appointment.Update(ConflictResolutionMode.AlwaysOverwrite); }
/** * Function Add the Holidays **/ static void AddHolidays(ExchangeService service, List<string[]> holidays, List<string> mailboxes) { // Log file string datetimeString = DateTime.Now.ToString("MMddyyyy"); string logfile = "../../logs/" + datetimeString + "_add_holiday_log.txt"; //Initiate Error List List<string> mbs = new List<string>(); using (System.IO.StreamWriter log = new System.IO.StreamWriter(@logfile, true)) { // Loop through each email address in the passed in mailboxes List foreach (string mailbox in mailboxes) { // Impersonate that User service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailbox); Console.WriteLine("Attempting to Add Holidays to: " + mailbox); List<Appointment> uga_holidays = new List<Appointment>(); // Loop through all the holidays foreach (string[] holiday in holidays) { //Create a new appointment Appointment appointment = new Appointment(service); // Set details appointment.Subject = holiday[0]; appointment.Start = DateTime.Parse(holiday[1]); appointment.End = appointment.Start.AddDays(1); appointment.IsAllDayEvent = true; StringList categories = new Microsoft.Exchange.WebServices.Data.StringList(); categories.Add("Holiday"); appointment.Categories = categories; appointment.IsReminderSet = false; uga_holidays.Add(appointment); } // Save and Send try { service.CreateItems(uga_holidays, WellKnownFolderName.Calendar, MessageDisposition.SaveOnly, SendInvitationsMode.SendToNone); Console.WriteLine("Added Holiday Successfully to: " + mailbox); DateTime now = DateTime.Now; log.WriteLine(now + " - Added holidays succesfully to Mailbox: " + mailbox); } catch (Exception ex) { Console.WriteLine("Error During Initial Add - Mailbox: " + mailbox + "; Exception thrown: " + ex); log.WriteLine("Error During Initial Add - Mailbox: " + mailbox + "; Exception thrown: " + ex); mbs.Add(mailbox); } // Clear impersonation. service.ImpersonatedUserId = null; } //Process Rerun List if (mbs.Count > 0) { Console.WriteLine("Looping through re-run mailboxes."); while (mbs.Count > 0) { // Current mailbox string mb = mbs.ElementAt(0); Console.WriteLine("On Mailbox: " + mb); // Take the mailbox out of the first element slot log.WriteLine("Removing mailbox " + mb + " from beginning of mbs."); mbs.RemoveAt(0); mbs.TrimExcess(); try { // Reruns: Removes // Run search // Impersonate that User service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mb); // Search String String category = "Holiday"; // Search Filter SearchFilter.IsEqualTo filter = new SearchFilter.IsEqualTo(AppointmentSchema.Categories, category); // Result Return Size, number of items ItemView holidayView = new ItemView(100); // Limit data to only necesary components holidayView.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Categories); FindItemsResults<Item> items = service.FindItems(WellKnownFolderName.Calendar, filter, holidayView); if (items.TotalCount > 0) { Console.WriteLine("Removing " + items.TotalCount + " holidays from " + mb); log.WriteLine("Found " + items.TotalCount + " holidays in the Calendar folder for " + mb + " to be removed."); List<ItemId> ids = new List<ItemId>(); foreach (Item item in items) { ids.Add(item.Id); } service.DeleteItems(ids, DeleteMode.MoveToDeletedItems, null, null); } else { log.WriteLine("Found no holidays in the Calendar folder for " + mb + " to be removed."); } // Rerun: Adds List<Appointment> holidays = new List<Appointment>(); // Loop through all the holidays foreach (string[] holiday in holidays) { //Create a new appointment Appointment appointment = new Appointment(service); // Set details appointment.Subject = holiday[0]; appointment.Start = DateTime.Parse(holiday[1]); appointment.End = appointment.Start.AddDays(1); appointment.IsAllDayEvent = true; StringList categories = new Microsoft.Exchange.WebServices.Data.StringList(); categories.Add("Holiday"); appointment.Categories = categories; appointment.IsReminderSet = false; holidays.Add(appointment); } service.CreateItems(holidays, null, null, SendInvitationsMode.SendToNone); Console.WriteLine("Added Holiday Successfully to" + mb); DateTime now = DateTime.Now; log.WriteLine(now + " - Added holidays succesfully to Mailbox: " + mb); } catch { log.WriteLine("Fatal Mailbox Errored on Re-Run Removes: " + mb + "; Will not retry."); Console.WriteLine("Fatal Mailbox Errored on Re-Run Removes: " + mb + "; Will not retry."); } // Clear impersonation. service.ImpersonatedUserId = null; } } } }