MS Graph API 使用 c# 在 Microsoft 365 日历事件上按关键字搜索

MS Graph API search by keyword on Microsoft 365 Calendar events using c#

我需要按关键字搜索,这样我就可以只检索包含我要搜索的关键字的事件。 我已经得到了解决部分问题的答案(按主题过滤), 但我仍然需要在 bodypreview 上搜索。

我正在尝试在这段代码中添加我的搜索(或过滤器)参数:

...

protected override async void OnAppearing()
    {
        base.OnAppearing();

        // Get start and end of week in user's time zone
        // I replaced variables below by pure strings on QueryOption <== DOES WORKS FINE
        //var startOfWeek = GetUtcStartOfWeekInTimeZone(DateTime.Today, App.UserTimeZone);
        //var endOfWeek = startOfWeek.AddDays(30);  //Eloir: original AddDays(7)

        var queryOptions = new List<QueryOption>
        {
            //new QueryOption("$search", "BodyPreview:Whosebug"),  <== DOES NOT WORK
            new QueryOption("startDateTime", "01/01/2020 00:00:00"),  
            new QueryOption("endDateTime", "31/12/2020 23:59:59")

        };

        var timeZoneString =
            Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.UWP ?
                App.UserTimeZone.StandardName : App.UserTimeZone.DisplayName;

        // Get the events
        var events = await App.GraphClient.Me.CalendarView.Request(queryOptions)
            .Header("Prefer", $"outlook.timezone=\"{timeZoneString}\"")
            //.Filter("BodyPreview contains 'Whosebug'")  <== DOES NOT WORK EITHER

            .Select(e => new
            {
                e.Subject,
                e.BodyPreview,
                e.Start,
                e.End
            })
            .OrderBy("start/DateTime")
            .Top(50)
            .GetAsync();

        // Add the events to the list view
        CalendarList.ItemsSource = events.CurrentPage.ToList();

    }

... 此代码是 Microsoft 文档站点的一部分: Build Xamarin apps with Microsoft Graph

CalendarView仅提供指定时间范围内的扩展事件列表,不支持额外过滤。

您可以使用 me/events 端点按 dateTimesubjectbodyPreview

进行过滤
GET https://graph.microsoft.com/v1.0/me/events?$filter=start/dateTime ge '2021-09-06T08:00' and end/dateTime lt '2021-09-30T08:00' and contains(subject,'planning')

C#

await App.GraphClient.Me.Events.Request()
    .Filter("start/dateTime ge '2021-09-06T08:00' and end/dateTime lt '2021-09-30T08:00' and contains(subject,'planning')")
    .Select(e => new
        {
            e.Subject,
            e.BodyPreview,
            e.Start,
            e.End
        })
        .OrderBy("start/DateTime")
        .Top(50)
        .GetAsync();