Partager via


Creating a recurring series by using the EWS Managed API 2.0

Last modified: September 12, 2013

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

You can use the Exchange Web Services (EWS) Managed API to create a recurring series of calendar items.

To create a recurring series

  1. Instantiate the Appointment object and identify the Exchange service. The following code shows how to create an appointment and provide it with connection configuration information by using an ExchangeService object named service.

    Appointment appointment = new Appointment(service);
    
  2. Set properties on the appointment and specify the recurrence pattern and recurrence range of the appointment. The following code shows how to add a subject, a body, a start time, an end time, and a location to an appointment, and how to specify that the appointment recurs every Saturday for ten weeks, effective January 1, 2009.

    appointment.Subject = "Weekly Tennis Lesson";
    appointment.Body = "My one hour tennis lesson is every Saturday at 10 A.M. for ten weeks.";
    appointment.Start = new DateTime(2009, 1, 1, 10, 0, 0);
    appointment.End = appointment.Start.AddHours(1);
    appointment.Location = "Redmond Pavillion";
    DayOfTheWeek[] days = new DayOfTheWeek[] { DayOfTheWeek.Saturday };
    appointment.Recurrence = new Recurrence.WeeklyPattern(appointment.Start.Date, 1, days);
    appointment.Recurrence.StartDate = appointment.Start.Date;
    appointment.Recurrence.NumberOfOccurrences = 10;
    
  3. Save the appointment. The following code shows how to save an appointment to the calendar. For information about the methods that you can use to save and send new meeting requests, see Creating appointments and meetings by using the EWS Managed API 2.0.

    appointment.Save(SendInvitationsMode.SendToNone);
    

Example

Creating a series with daily recurrence

The following code example shows how to create a recurring meeting series and a recurring appointment series. The meeting recurs daily for two weeks, starting on January 1, 2009. The appointment recurs every third day starting on January 1, 2009, through March 31, 2009.

// Create the meeting. 
Appointment meeting = new Appointment(service);

// Set properties on the meeting.
meeting.Subject = "Daily Meeting";
meeting.Body = "This one hour meeting will recur daily for two weeks, starting on January 1, 2009.";
meeting.Start = new DateTime(2009, 1, 1, 10, 0, 0);
meeting.End = meeting.Start.AddHours(1);
meeting.Location = "Conf Room 1";
meeting.RequiredAttendees.Add("User1@contoso.com");

// Set the recurrence information on the meeting.
// The meeting will recur daily for two weeks, starting on January 1, 2009.
meeting.Recurrence = new Recurrence.DailyPattern(meeting.Start.Date, 1);
meeting.Recurrence.StartDate = meeting.Start.Date;
meeting.Recurrence.EndDate = (meeting.Start.Date).AddDays(13);

// Create the meeting and send the meeting invitation to attendees.
meeting.Save(SendInvitationsMode.SendOnlyToAll);

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Appointment - Every Third Day";
appointment.Body = "This one hour appointment will recur every third day, starting on January 1, 2009, and continuing through March 31, 2009.";
appointment.Start = new DateTime(2009, 1, 1, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur every third day, starting on Januray 1, 2009, and continuing through March 31, 2009.
appointment.Recurrence = new Recurrence.DailyPattern(appointment.Start.Date, 3);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.EndDate = new DateTime(2009, 3, 31);

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Creating a series with weekly recurrence

The following code example shows how to create a recurring meeting series and a recurring appointment series. The meeting recurs every Monday, effective January 1, 2009, and continuing indefinitely. The appointment recurs on Monday and Wednesday of every second week effective January 1, 2009, through March 31, 2009.

// Create the meeting. 
Appointment meeting = new Appointment(service);

// Set properties on the meeting.
meeting.Subject = "Weekly Meeting on Monday";
meeting.Body = "This one hour meeting will recur every Monday, effective January 1, 2009, and continuing indefinitely.";
meeting.Start = new DateTime(2009, 1, 1, 10, 0, 0);
meeting.End = meeting.Start.AddHours(1);
meeting.Location = "Conf Room 1";
meeting.RequiredAttendees.Add("User1@contoso.com");

// Set the recurrence information on the meeting.
// The meeting will recur every Monday, effective January 1, 2009, and continuing indefinitely.
DayOfTheWeek[] days = new DayOfTheWeek[] { DayOfTheWeek.Monday };
meeting.Recurrence = new Recurrence.WeeklyPattern(meeting.Start.Date, 1, days);
meeting.Recurrence.StartDate = meeting.Start.Date;

// Create the meeting and send the meeting invitation to attendees.
meeting.Save(SendInvitationsMode.SendOnlyToAll);

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Bi-Weekly Appointment (Monday, Wednesday)";
appointment.Body = "This one hour appointment will recur on Monday and Wednesday of every second week, effective January 1, 2009, through March 31, 2009.";
appointment.Start = new DateTime(2009, 1, 1, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur on Monday and Wednesday of every second week, effective January 1, 2009, through March 31, 2009.
DayOfTheWeek[] days2 = new DayOfTheWeek[] { DayOfTheWeek.Monday, DayOfTheWeek.Wednesday };
appointment.Recurrence = new Recurrence.WeeklyPattern(appointment.Start.Date, 2, days2);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.EndDate = new DateTime(2009, 3, 31);

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Creating a series with monthly recurrence

The following code example shows how to create a recurring meeting series and a recurring appointment series. The meeting recurs on the 15th of each month, effective January 1, 2009, and continuing indefinitely. The appointment recurs on the 20th of every third month, effective January 1, 2009 through December 31, 2009.

// Create the meeting. 
Appointment meeting = new Appointment(service);

// Set properties on the meeting.
meeting.Subject = "Monthly Meeting";
meeting.Body = "This one hour meeting will recur on the 15th of each month, effective January 1, 2009, and continuing indefinitely.";
meeting.Start = new DateTime(2009, 1, 1, 10, 0, 0);
meeting.End = meeting.Start.AddHours(1);
meeting.Location = "Conf Room 1";
meeting.RequiredAttendees.Add("User1@contoso.com");

// Set the recurrence information on the meeting.
// The meeting will recur on the 15th of each month, effective January 1, 2009, and continuing indefinitely.
meeting.Recurrence = new Recurrence.MonthlyPattern(meeting.Start.Date, 1, 15);
meeting.Recurrence.StartDate = meeting.Start.Date;
meeting.Recurrence.NeverEnds();

// Create the meeting and send the meeting invitation to attendees.
meeting.Save(SendInvitationsMode.SendOnlyToAll);

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Quarterly Appointment";
appointment.Body = "This one hour appointment will recur on the 20th of every third month, effective January 1, 2009 through December 31, 2009.";
appointment.Start = new DateTime(2009, 1, 1, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur on the 20th of every third month, effective January 1, 2009 through December 31, 2009.
appointment.Recurrence = new Recurrence.MonthlyPattern(appointment.Start.Date, 3, 20);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.EndDate = new DateTime(2009, 12, 31);

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Creating a series with relative monthly recurrence

The following code example shows how to create a recurring meeting series and a recurring appointment series. The meeting recurs on the third Monday of each month, effective January 1, 2009, and continuing indefinitely. The appointment recurs on the last Thursday of every second month, effective January 1, 2009 through December 31, 2009.

// Create the meeting. 
Appointment meeting = new Appointment(service);

// Set properties on the meeting.
meeting.Subject = "Monthly Meeting (third Monday)";
meeting.Body = "This one hour meeting will recur on the third Monday of each month, effective January 1, 2009, and continuing indefinitely.";
meeting.Start = new DateTime(2009, 1, 1, 10, 0, 0);
meeting.End = meeting.Start.AddHours(1);
meeting.Location = "Conf Room 1";
meeting.RequiredAttendees.Add("User1@contoso.com");

// Set the recurrence information on the meeting.
// The meeting will recur on the third Monday of each month, effective January 1, 2009, and continuing indefinitely.
meeting.Recurrence = new Recurrence.RelativeMonthlyPattern(meeting.Start.Date, 1, DayOfWeek.Monday, DayOfWeekIndex.Third);
meeting.Recurrence.StartDate = meeting.Start.Date;
meeting.Recurrence.NeverEnds();

// Create the meeting and send the meeting invitation to attendees.
meeting.Save(SendInvitationsMode.SendOnlyToAll);

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Bi-Monthly Appointment (last Thursday)";
appointment.Body = "This one hour appointment will recur on the last Thursday of every second month, effective January 1, 2009, and continuing through December 31, 2009.";
appointment.Start = new DateTime(2009, 1, 1, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur on the last Thursday of every second month, effective January 1, 2009, and continuing through December 31, 2009.
appointment.Recurrence = new Recurrence.RelativeMonthlyPattern(appointment.Start.Date, 2, DayOfWeek.Thursday, DayOfWeekIndex.Last);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.EndDate = new DateTime(2009, 12, 31);

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Creating a series with yearly recurrence

The following code example shows how to create a recurring appointment series. The appointment recurs on November 15th of each year, starting on November 15, 2009, and continuing indefinitely.

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Yearly Appointment";
appointment.Body = "This one hour appointment will recur each year on November 15th, starting on November 15, 2009, and continuing indefinitely.";
appointment.Start = new DateTime(2009, 11, 15, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur each year on November 15th, starting on November 15, 2009, and continuing indefinitely.
appointment.Recurrence = new Recurrence.YearlyPattern(appointment.Start.Date, Month.November, 15);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.NeverEnds();

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Creating a series with relative yearly recurrence

The following code example shows how to create a recurring appointment series. The appointment recurs on the first Monday in November each year for five years, starting in November 2009.

// Create the appointment. 
Appointment appointment = new Appointment(service);

// Set properties on the appointment.
appointment.Subject = "Yearly Appointment";
appointment.Body = "This one hour appointment will recur on the first Monday in November each year for five years, starting in November 2009.";
appointment.Start = new DateTime(2009, 11, 1, 10, 0, 0);
appointment.End = appointment.Start.AddHours(1);

// Set the recurrence information on the appointment.
// The appointment will recur on the first Monday in November each year for five years, starting in November 2009.
appointment.Recurrence = new Recurrence.RelativeYearlyPattern(appointment.Start.Date, Month.November, DayOfWeek.Monday, DayOfWeekIndex.First);
appointment.Recurrence.StartDate = appointment.Start.Date;
appointment.Recurrence.NumberOfOccurrences = 5;

// Save the recurring series.
appointment.Save(SendInvitationsMode.SendToNone);

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.