3
votes

I'm using EWS Managed API 2.0. I would like to be able to search a calendar in exchange on the subject of the appointment in the future.

The caveats are:

  1. only return future appointments matching the subject="test"
  2. only return future appointments in the next 90 days

I can get CalendarView to return appointments in the next 90 days, but cannot figure out how to filter using a SearchFilter. For best performance, I'd rather not return all appointments and then filter.

I can filter the appointments by Subject using ItemView and a SearchFilter. However this doesn't filter out appointments that have already occurred. It returns everything matching the filter.

Ideally, it would be nice if I could use a CalendarView in the SearchFilter but I receive the error "Restrictions and sort order may not be specified for a CalendarView."

FindItemsResults<Item> findResults = svc().FindItems(fId, filter, cView);

Any help would be great... thank you!

2

2 Answers

3
votes

I figured it out....

Using compound search filters, like so

        SearchFilter.SearchFilterCollection coll = new SearchFilter.SearchFilterCollection(LogicalOperator.And);            
        SearchFilter subjectFilter = new SearchFilter.ContainsSubstring(AppointmentSchema.Subject, "test");
        SearchFilter dateFilter = new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, DateTime.Today);
        coll.Add(subjectFilter);
        coll.Add(dateFilter);

        FindItemsResults<Item> findResults = svc().FindItems(fId, coll, view);
1
votes

Beware, when using compound search filters you will not get occurrences of a recurring series if the master element is outside of the specified time range.

This is because occurrences (and exceptions) in a recurring series are not actual items in the mailbox, but rather are stored internally as attachments to a recurring master. The ExchangeService.FindItems method does not look through the attachment table of every calendar item to find occurrences and exceptions. (Source: http://msdn.microsoft.com/en-us/library/office/dn495614(v=exchg.150).aspx#bk_recurring)

So if you're dealing with recurring events you're (IMHO) better off using a CalendarView and the CalendarFolder.FindAppointments method, which performs recurrence expansion for recurring appointments.

The downside is that it does not support search filters... So you must filter subjects seperately.

Does anybody have a better solution? Any help would be greatly appreciated. Thanks.