James Blake
James Blake

Reputation: 1118

Get recurrence pattern for appointment using EWS Managed API 1.2

I am looking for the correct way to get the recurrence pattern associated with an appointment using EWS Managed API 1.2. My code looks something like this:

FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Calendar, view);

foreach (Appointment appointment in findResults)
{
    appointment.Load();

    if (appointment.IsRecurring)
    {
        // What is the recurrence pattern???
    }
}

I can do a appointment.Recurrence.ToString() and I get back like Microsoft.Exchange.WebServices.Data.Recurrence+WeeklyPattern. Obviously I could parse that and determine the type, but that doesn't seem very clean. Is there a better way?

There is another post similar to this here - EWS: Accessing an appointments recurrence pattern but the solution does not seem complete.

Upvotes: 2

Views: 3719

Answers (4)

Michel Butz
Michel Butz

Reputation: 1

I have another way to solve the problem in my project. For me it's more clearly easier to read but that's matter of taste.

Appointment app = Appointment.Bind(service,id);
string[] split = app.Recurrence.ToString().Split('+');
if (split.Length != 2)
  return;

string pattern = split[1];

switch (pattern)
{
  case "DailyPattern":
    break;

  case "WeeklyPattern":
    break;

  case "MonthlyPattern":
    break;

  case "YearlyPattern":
    break;
}

Upvotes: 0

Kai Hartmann
Kai Hartmann

Reputation: 3154

My 2 cents on this. I would implement it by checking the type:

if(app.Recurrence.GetType() == typeof(Recurrence.DailyPattern))
{
    // do something 
}
else if(app.Recurrence.GetType() == typeof(Recurrence.WeeklyPattern))
{
    // do something
}
...

Upvotes: 1

J&#252;rgen Hoffmann
J&#252;rgen Hoffmann

Reputation: 957

here's a full list of the patterns. Cause of there is no Property you could as what pattern is in use, you will have to cast the recurrence to the pattern. in my project I solved this problem this way:

Appointment app = Appointment.Bind(service,id);
Recurrence.DailyPattern dp = app.Recurrence as Recurrence.DailyPattern;
Recurrence.DailyRegenerationPattern drp = app.Recurrence as Recurrence.DailyRegenerationPattern;
Recurrence.MonthlyPattern mp = app.Recurrence as Recurrence.MonthlyPattern;
Recurrence.MonthlyRegenerationPattern mrp = app.Recurrence as Recurrence.MonthlyRegenerationPattern;
Recurrence.RelativeMonthlyPattern rmp = app.Recurrence as Recurrence.RelativeMonthlyPattern;
Recurrence.RelativeYearlyPattern ryp = app.Recurrence as Recurrence.RelativeYearlyPattern;
Recurrence.WeeklyPattern wp = app.Recurrence as Recurrence.WeeklyPattern;
Recurrence.WeeklyRegenerationPattern wrp = app.Recurrence as Recurrence.WeeklyRegenerationPattern;
Recurrence.YearlyPattern yp = app.Recurrence as Recurrence.YearlyPattern;
Recurrence.YearlyRegenerationPattern yrp = app.Recurrence as Recurrence.YearlyRegenerationPattern;

if (dp != null)
{ 
//Do something
}
else if (drp != null)
{
//Do something
}
else if (mp != null)
{
//Do something
}
else if (mrp != null)
{
//Do something
}
else if (rmp != null)
{
//Do something
}
else if (ryp != null)
{
//Do something
}
else if (wp != null)
{
//Do something
}
else if (wrp != null)
{
//Do something
}
else if (yp != null)
{
//Do something
}
else if (yrp != null)
{
//Do something
}

hope that helps you...

Upvotes: 2

9T9
9T9

Reputation: 698

Microsoft.Exchange.WebServices.Data.Recurrence.IntervalPattern pattern = (Microsoft.Exchange.WebServices.Data.Recurrence.IntervalPattern)microsoftAppointment.Recurrence;

Is this what you are looking for?

Upvotes: 1

Related Questions