Deleting calendar events with Microsoft Graph - Why do I need to call multiple times?

前端 未结 1 668
盖世英雄少女心
盖世英雄少女心 2021-01-26 20:59

After much investigation and beta-testing I have come up with the following approach to delete calendar events:

public async Task DeleteCalendarEvent         


        
相关标签:
1条回答
  • 2021-01-26 21:45

    If I use the $top option I can adjust the size of the pages and thus reduce the number of calls I need to make:

    List<QueryOption> listQueryOptions = new List<QueryOption>()
    {
        new QueryOption("$top", "50")
    };
    
    var oEvents = await _graphClient
                            .Me
                            .Calendars[oSettings.CalendarID]
                            .Events
                            .Request(listQueryOptions)
                            .Filter(strFilter)
                            .OrderBy("start/DateTime")
                            .GetAsync();
    

    This can be simplified that further:

    var oEvents = await _graphClient
                            .Me
                            .Calendars[oSettings.CalendarID]
                            .Events
                            .Request()
                            .Top(50)
                            .Filter(strFilter)
                            .OrderBy("start/DateTime")
                            .GetAsync();
    

    This resource was very handy. In the end I have:

    public async Task<bool> DeleteCalendarEvents(SettingsBase oSettings)
    {
        try
        {
            var oEvents = await _graphClient
                                    .Me
                                    .Calendars[oSettings.CalendarID]
                                    .Events
                                    .Request()
                                    .Select("Start,Subject,Id") // TODO: Is there any point doing this?
                                    .Top(50)
                                    .Filter(oSettings.GetFilterString())
                                    .OrderBy("start/DateTime")
                                    .GetAsync();
    
            List<Event> listEvents = new List<Event>();
    
            listEvents.AddRange(oEvents);
            while(oEvents.NextPageRequest != null)
            {
                oEvents = await oEvents.NextPageRequest.GetAsync();
                listEvents.AddRange(oEvents);
            }
    
            foreach(Event oEvent in listEvents)
            { 
                await _graphClient.Me.Events[oEvent.Id].Request().DeleteAsync();
            }
    
        }
        catch(Exception ex)
        {
            SimpleLog.Log(ex);
            Console.WriteLine("DeleteCalendarEvents: See error log.");
            return false;
        }
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题