After much investigation and beta-testing I have come up with the following approach to delete calendar events:
public async Task DeleteCalendarEvent
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;
}