Search code examples
c#office365office365-appsoffice365apioutlook-restapi

Get events of a specific time range


I'm working with the Office365 Outlook Calendar API. I need to get events of a specific time range. I tried to compare the DateTimeTimeZone values inside of the foreach command, but it seems like it only supports a == operator:

if ( calendarEvent.Start >= new DateTimeTimeZone()
            {
                TimeZone = TimeZoneInfo.Local.Id,
                DateTime = DateTime.Now.ToString("s")
            })

This code snippet fails with the error: Cannot apply operator '>=' to operands of type 'Microsoft.Office365.OutlookServices.DateTimeTimeZone' and 'Microsoft.Office365.OutlookServices.DateTimeTimeZone'

Are there any other ways get events of a specific time range, e.g. future events?

This is my GetEvents()-method so far:

    [Route("GetEvents")]
    public async Task GetEvents()
    {
        //Get client
        OutlookServicesClient client = await this.GetClient();

        //Get events
        var events = await client.Me.Events
            .Take(10)
            .ExecuteAsync();

        foreach (var calendarEvent in events.CurrentPage)
        {
            //
            if ( calendarEvent.Subject == "Test 1" )
            {
                System.Diagnostics.Debug.WriteLine("Event '{0}'.", calendarEvent.Subject) ; 
            }
        }
    }

Solution

  • You should use the CalendarView to get events within a time range.

    DateTimeOffset startDateTime, endDateTime;
    var events = client.Me.GetCalendarView(startDateTime, endDateTime);