简体   繁体   中英

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.

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...

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

这是你想要的?

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
}
...

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM