13

If I do this:

_calendar = (CalendarFolder)Folder.Bind(_service, WellKnownFolderName.Calendar);

var findResults = _calendar.FindAppointments(
    new CalendarView(startDate.Date, endDate.Date)
);

I sometimes get an exception that too many items were found.

"You have exceeded the maximum number of objects that can be returned for the find operation. Use paging to reduce the result size and try your request again."

CalendarView supports a constructor that will let me specify MaxItemsReturned, but I can't figure out how I would call it again, specifying the offset for paging. ItemView has this constructor:

 public ItemView(int pageSize, int offset)

And the usage of that is obvious.

What about CalendarView? How does one do paging with a CalendarView?

I could reduce the date range to be a shorter span, but there's still no way of determining if it will work for sure.

3 Answers 3

8

CalendarView is not actually derived from PagedView, so all of the paging logic that you expect isn't possible. MaxItemsReturned is more of an upper limit than a page size. The error that's returned is more relevant to the PagedView derived view types.

I played around with some PowerShell to emulate paging by rolling the CalendarView window based on the last item returned, but unfortunately the logic behind the CalendarView and Appointment expansion make it impossible to get exactly what you need. Basically as it does the expansion, it's going to stop at "N" items, but you might have more than one appointment that starts at the exact same time and it may give you one, but not the rest. Also, any appointments that overlap the window will get included, so the below code would go into an infinite loop if you had 50 appointments on the calendar that all had the same start time.

Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$cred = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials ($user , $passwd)
$service.UseDefaultCredentials = $false
$service.Credentials = $cred
$service.AutodiscoverUrl($user)

$num=50
$total=0
$propsetfc = [Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties
$calfolder = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar

$service.UserAgent = "EWSCalViewTest"
$calview = New-Object Microsoft.Exchange.WebServices.Data.CalendarView("1/1/2012","12/31/2012", $num)
$calview.PropertySet = $propsetfc

do {
    $findresults = $service.FindAppointments($calfolder,$calview)
    write-host  "Found:" $findresults.Items.Count "of" $findresults.TotalCount
    $calview.StartDate = $findresults.Items[$findresults.Items.Count-1].Start
    $total+=$findresults.Items.Count
} while($findresults.MoreAvailable)
write-host $total "total found (including dups)"

Unfortunately the expansion and overlap logic mean you'll get duplicates this way, at least one duplicate for each call beyond the first.

If I had to write code using CalendarView, I'd probably use a MaxItemsReturned of 1000 (this is also the limit that throws you into the error condition if you don't specify MaxItemsReturned). If you get them all in one call, you're good. If you have to make a second call, then you'll have to do some extra work to dedup the result set. I'd also try to limit the burden on the server by using as narrow of a date window as possible in the CalendarView since you're asking Exchange to calculate the expansion of recurring appointments across the entire time span. It can be a fairly expensive operation for the server.

5
  • Good stuff RickH, but still not deterministic enough for me. What I've done in the meantime is break the span into months and use CalendarView for each month. Seems to be working fine.
    – tig
    Aug 19, 2012 at 23:44
  • The 'server processing cost' is basically the same regardless if I do it for a full year or with 12 month long ranges, no?
    – tig
    Aug 19, 2012 at 23:45
  • Even though your response doesn't really solve my problem, you do provide an answer: It is not possible to do PagedmView style paging with a calendar. So marking this as the answer.
    – tig
    Aug 19, 2012 at 23:46
  • For the month-by-month split, if you have appointments that span the month boundary you'll get duplicates of those appointments. If you need the whole year, I'd say that the two options are roughly equal with the 12 separate calls using slightly more resources due to the additional overhead of the separate calls, and of doing 12 smaller expansion cache lookups / expansions.
    – RickH
    Aug 20, 2012 at 13:02
  • If the calendar have more than MaxItemsReturned appointments starting at the same time, it will get stuck in an infinite loop. Jan 28, 2014 at 11:36
2

You can use ItemView and SearchFilter to query appointments:

var itemView = new ItemView(100, 0);
itemView.PropertySet = new PropertySet(BasePropertySet.IdOnly,
    ItemSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
    new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "IPM.Appointment"),
    new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, startDate),
    new SearchFilter.IsLessThan(AppointmentSchema.Start, endDate));

bool moreAvailable = true;
while (moreAvailable)
{
    var result = _service.FindItems(WellKnownFolderName.Calendar, filter, itemView);

    foreach (var appointment in result.OfType<Appointment>())
    {
        DateTime start = appointment.Start;
        DateTime end = appointment.End;
        string subject = appointment.Subject;

        // ...
    }

    itemView.Offset += itemView.PageSize;
    moreAvailable = result.MoreAvailable;
}
1
  • 4
    This does not expand the occurrences of recurring masters and thus provides an incomplete view of the calender as far as I can tell. That's why the CalendarView is so useful (except for the fact that it doesn't page well) Jan 30, 2014 at 11:54
1

You can still paginate the FindAppointments function manipulating the CalendarView start dates.

var cal = CalendarFolder.Bind(_service, WellKnownFolderName.Calendar);
var cv = new CalendarView(start, end, 1000);

var appointments = new List<Appointment>();

var result = cal.FindAppointments(cv);

appointments.AddRange(result);

while (result.MoreAvailable)
{
     cv.StartDate = appointments.Last().Start;

     result = cal.FindAppointments(cv);

     appointments.AddRange(result);
}

Though I don't know if they come in order. If they don't you might have to use the last envent start date and remove the duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.