Update a recurring series by using EWS in Exchange
Learn how to update an entire recurring series at once by using the EWS Managed API or EWS in Exchange.
You can use the EWS Managed API or EWS to update a recurring series by either updating the entire series, or by updating a single occurrence. In this article we'll discuss how to update the entire series at once.
In general, updating a recurring series is very similar to modifying a single appointment. You use the same methods and operations, but you use the item ID of the series' recurring master. In some cases you might not start with the recurring master, and you might need to find the item ID for the recurring master.
However, there is one key difference to consider when updating a recurring series: updating the recurrence pattern. Updating the recurrence pattern is only possible with the recurring master, and changes to the pattern can add or remove occurrences. For example, if you modify the Recurrence.EndDate property to a date later than its current value, the recurrence pattern is reevaluated, and additional occurrences might be added.
Modify all occurrences in a series by using the EWS Managed API
To modify all occurrences in a series you:
- Bind to the recurring master for the series by using the Appointment.BindToRecurringMaster or Appointment.Bind method on a recurring master.
- Update the properties on the recurring master Appointment object.
- Save the changes to the recurring master by using the Appointment.Save method.
The following example updates a recurring series to change the location, add an attendee, and modify the recurrence pattern. This example assumes that the ExchangeService object passed in the service parameter has been initialized with valid values in the Credentials and Url properties. The recurringAppointment parameter is an Appointment object bound to either an occurrence or the recurring master for the series to be updated.
using Microsoft.Exchange.WebServices.Data;
public static bool UpdateRecurringSeries(ExchangeService service, Appointment recurringAppointment)
{
Appointment recurringMaster = null;
// If the item is a single appointment, fail.
if (recurringAppointment.AppointmentType == AppointmentType.Single)
{
Console.WriteLine("ERROR: The item to delete is not part of a recurring series.");
return false;
}
// Check the Appointment that was passed. Is it
// an occurrence or the recurring master?
if (recurringAppointment.AppointmentType != AppointmentType.RecurringMaster)
{
// If an occurrence was passed in, bind to the master.
try
{
// This method results in a call to EWS.
recurringMaster = Appointment.BindToRecurringMaster(service, recurringAppointment.Id);
}
catch (Exception ex)
{
Console.WriteLine("Couldn't bind to master: {0}", ex.Message);
return false;
}
}
else
{
// Bind to the appointment to load all properties.
// This method results in a call to EWS.
recurringMaster = Appointment.Bind(service, recurringAppointment.Id);
}
// Basic updates. These kinds of updates are the same
// as if you were updating a single appointment.
// Update the location. All occurrences will update to this new location.
recurringMaster.Location = "Conference Room 2";
// Add an attendee.
Attendee newAttendee = new Attendee("sadie@contoso.com");
recurringMaster.RequiredAttendees.Add(newAttendee);
// Changes to the recurrence. This is only applicable to a recurring
// master.
// If the series has an end date, extend the series to add two more occurrences.
if (recurringMaster.Recurrence.HasEnd)
{
// NumberOfOccurrences is only set if the user created the
// appointment with a set number of occurrences.
// Otherwise, there's a start and end date.
if (recurringMaster.Recurrence.NumberOfOccurrences != null)
{
recurringMaster.Recurrence.NumberOfOccurrences += 2;
}
else
{
// This is a bit more complicated if you want to add two more
// occurrences. You need to calculate a new end date.
Type recurrenceType = recurringMaster.Recurrence.GetType();
switch (recurrenceType.Name)
{
case "DailyPattern":
recurringMaster.Recurrence.EndDate =
recurringMaster.Recurrence.EndDate.Value.AddDays(2);
break;
case "WeeklyPattern":
recurringMaster.Recurrence.EndDate =
recurringMaster.Recurrence.EndDate.Value.AddDays(14);
break;
case "YearlyPattern":
recurringMaster.Recurrence.EndDate =
recurringMaster.Recurrence.EndDate.Value.AddYears(2);
break;
default:
// Do nothing here. There are other recurrence
// types but for simplicity, these aren't covered.
break;
}
}
}
else
{
// If it has no end, set an end date to 2 weeks from today.
recurringMaster.Recurrence.EndDate = DateTime.Now.AddDays(14);
}
// Update the series.
try
{
// This method results in a call to EWS.
recurringMaster.Update(ConflictResolutionMode.AutoResolve);
}
catch (Exception ex)
{
Console.WriteLine("Error updating series: {0}", ex.Message);
return false;
}
return true;
}
Modify all occurrences in a series by using EWS
To modify all occurrences in a series, you need to use the UpdateItem operation with the item ID of the recurring master in the ItemId element in the request. The structure of the request is the same as a request to update a single appointment.
The following example updates the recurring series in the following ways:
- Updates the location of the series by setting the Location element.
- Updates the attendees by setting the RequiredAttendees element.
- Updates the recurrence by setting the Recurrence (RecurrenceType) element.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2007_SP1" />
<t:TimeZoneContext>
<t:TimeZoneDefinition Id="Eastern Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve" SendMeetingInvitationsOrCancellations="SendToAllAndSaveCopy">
<m:ItemChanges>
<t:ItemChange>
<t:ItemId Id="AAMkADA5..." ChangeKey="DwAAABYA..." />
<t:Updates>
<t:SetItemField>
<t:FieldURI FieldURI="calendar:Location" />
<t:CalendarItem>
<t:Location>Conference Room 2</t:Location>
</t:CalendarItem>
</t:SetItemField>
<t:SetItemField>
<t:FieldURI FieldURI="calendar:RequiredAttendees" />
<t:CalendarItem>
<t:RequiredAttendees>
<t:Attendee>
<t:Mailbox>
<t:Name>Mack Chaves</t:Name>
<t:EmailAddress>mack@contoso.com</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
</t:Mailbox>
</t:Attendee>
<t:Attendee>
<t:Mailbox>
<t:Name>Sadie Daniels</t:Name>
<t:EmailAddress>sadie@contoso.com</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
</t:Mailbox>
</t:Attendee>
</t:RequiredAttendees>
</t:CalendarItem>
</t:SetItemField>
<t:SetItemField>
<t:FieldURI FieldURI="calendar:Recurrence" />
<t:CalendarItem>
<t:Recurrence>
<t:WeeklyRecurrence>
<t:Interval>1</t:Interval>
<t:DaysOfWeek>Tuesday</t:DaysOfWeek>
</t:WeeklyRecurrence>
<t:EndDateRecurrence>
<t:StartDate>2014-05-06</t:StartDate>
<t:EndDate>2014-06-22-04:00</t:EndDate>
</t:EndDateRecurrence>
</t:Recurrence>
</t:CalendarItem>
</t:SetItemField>
<t:SetItemField>
<t:FieldURI FieldURI="calendar:MeetingTimeZone" />
<t:CalendarItem>
<t:MeetingTimeZone TimeZoneName="Pacific Standard Time" />
</t:CalendarItem>
</t:SetItemField>
</t:Updates>
</t:ItemChange>
</m:ItemChanges>
</m:UpdateItem>
</soap:Body>
</soap:Envelope>
The server responds with an UpdateItemResponse element that includes a ResponseCode element with a value of NoError, which indicates that the update was successful.