How to get the recurring master of all recurring series that have one or more occurrences that start within in a date range using FindItems()?

StackOverflow https://stackoverflow.com/questions/22464784

  •  16-06-2023
  •  | 
  •  

Question

Background:

I am using the Microsoft Exchange Web Services Managed API 2.0. I am attempting to search a Calendar folder and return all appointments items that meet the following criteria:

  • Has a sensitivity of Normal
  • Has a category of "Test"
  • Starts in a specific date range

I have come to the conclusion that due to the fact that I need to filter on more than just the date I need to use FindItems() instead of FindAppoinments().(Please correct me if that is wrong.) The downside of FindItems() is that it only returns the master of a recurring series and I will need to explode the occurrences myself. I am exploding the master without issue, however in my testing a ran across a problem in the way FindItems() searches recurrences. It only seems to return the master of a recurring series if the entire series starts sometime within my search range. So if someone has a recurring series set daily for the next year and I search the calendar next month, FindItems() will not give any indication that there is a recurring series happening within that range.

TLDR:

Given a calendar with a recurring series that has a daily frequency starting 1/1/2014 and ending 1/30/2014. How can I use FindItems() with a filter date range of 1/10/2014 - 1/20/2014 to return the recurring master for that series?

My Code

// A search collection that contains all of the search conditions.
List<SearchFilter> masterSearchFilterCollection = new List<SearchFilter>();
masterSearchFilterCollection.Add(new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "IPM.Appointment"));

masterSearchFilterCollection.Add(new SearchFilter.IsEqualTo(AppointmentSchema.Sensitivity, Sensitivity.Normal)); //No Private items
//masterSearchFilterCollection.Add(new SearchFilter.ContainsSubstring(AppointmentSchema.Categories, "Test"));

List<SearchFilter> dateRangeSearchFilterCollection = new List<SearchFilter>();
dateRangeSearchFilterCollection.Add(new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, searchStartDateTime));
dateRangeSearchFilterCollection.Add(new SearchFilter.IsLessThanOrEqualTo(AppointmentSchema.Start, searchEndDateTime));
masterSearchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, dateRangeSearchFilterCollection));

SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, masterSearchFilterCollection);

ItemView view = new ItemView(pageSize, initialOffset);
view.PropertySet = GetPrimaryProperties();
FindItemsResults<Item> results = Service.FindItems(Folder, searchFilter, view);

foreach(Appointment item in results)
{
   if (item.AppointmentType == AppointmentType.RecurringMaster)
   {
      // Calendar item is a recurring master item for a recurring series.
      // Loop through all occurrences of the master here
   }
   else
   {
      // Calendar item is not part of a recurring series.
   }
}
Was it helpful?

Solution

John,

In order to find your recurring master appointment you will need to use the FindAppointments() method. Specifying the start and end dates in this method will allow you see any recurring appointment that spans the date range. You can then filter your appointments by checking the Sensitivity and Categories properties of these appointments

Once you find an appointment that meets your criteria, check the AppointmentType property to determine what to do next. If it's an Occurrence or Exception then you can use the Appointment.BindToRecurringMaster() method to get to your recurring master. Here's an example:

switch (calendarItem.AppointmentType)
{
    case AppointmentType.RecurringMaster:
       // Nothing to do here since you are already on the recurring master
       break;

    case AppointmentType.Single:
       // This is not a recurring series
       break;

    case AppointmentType.Occurrence:
       // We need to get to the recurring master
       Appointment recurringMaster = Appointment.BindToRecurringMaster(service, calendarItem.Id);
       break;

    case AppointmentType.Exception:
       // We need to get to the recurring master
       Appointment recurringMaster = Appointment.BindToRecurringMaster(service, calendarItem.Id);
       break;

}

Now that you have a reference to your recurring master you can loop through the occurrences as you did before.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top