简体   繁体   中英

Office 365 Calendar API - Filtering Events by Date in C# Client Libraries

In Client Libraries you can apply LINQ filtering on calendar event request:

var events = await (from i in Client.Me.Events where i.Subject == "Desired Event Name" select i)
    .Take(50)
    .ExecuteAsync();

Or one can use Where method, however for Start and End fields when we want events from specific time period filtering cannot be used as the DateTime s are stored as strings. Invocation of DateTime.Parse method causes an exception. This is definitely supposed to be achievable, I even think it was possible at some point and can be done with REST. The begin/finish property is indexed according to documentation. Of course the results can be filtered once received but in that case I started getting events four years old. It really takes a lot of time to get through all the pages in the IPagedCollection in this approach. Fortunately though the events appear to be ordered by date, so you can stop the acquisition of new pages once events begin after your period of time.

Me. I still did not find solution to querying events with LINQ. To view a specified interval one can use Client.Me.CalendarView(from as DateTimeOffset, to as DateTimeOffset) or Client.Me.Calendars["<valid calendar id>"].CalendarView(from, to) . Filtering by function is probably exclusive to client-side code.

Which version of Office 365 REST API you were initializing the OutLookServicesClient? I can filter the events use the v1.0 API. You can refer the code below to use the LINQ to filter the with start and end property:

   OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v1.0/"), () =>
        {
            return Task.Delay(10).ContinueWith(t => accessToken);
        });



        var events = await (from i in client.Me.Events where (i.Start > DateTimeOffset.Parse("2016-07-18") && i.End< DateTimeOffset.Parse("2016-07-25")) select i)
                        .Take(50)
                        .ExecuteAsync();

        foreach (var appointment in events.CurrentPage)
        {
            Console.WriteLine($"{appointment.Subject}:\t{appointment.Start}~{appointment.End}");
        }

Update(V2.0)

Install the V2.0 manage assembly Install-Package Microsoft.Office365.OutlookServices-V2.0

Code:

  OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0/"), () =>
        {
            return Task.Delay(10).ContinueWith(t => accessToken);
        });

        var events = await (from i in client.Me.Events where (i.Start.DateTime.CompareTo("2016-07-18")>0 && i.End.DateTime.CompareTo("2016-07-25")<0) select i)
                  .Take(50)
                  .ExecuteAsync();

        foreach (var appointment in events.CurrentPage)
        {
            Console.WriteLine($"{appointment.Subject}:\t{appointment.Start}~{appointment.End}");
        }

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