经过大量调查和测试后,我提出了以下方法来删除日历事件:
public async Task<bool> DeleteCalendarEvents(SettingsBase oSettings, string valueProperty1, string valueProperty2)
{
string strFilterDateRange = String.Format("start/dateTime ge '{0}T00:00' and end/dateTime le '{1}T23:59'",
oSettings.StartDate.ToString("yyyy-MM-dd"),
oSettings.EndDate.ToString("yyyy-MM-dd"));
// Filter the events on the TruckleSoft1 property
string strFilterProperty1 = $"singleValueExtendedProperties/Any(ep: ep/id eq '{SettingsBase.Property_TruckleSoft1}' and ep/value eq '{valueProperty1}')";
// Build the filter
string strFilter = $"{strFilterDateRange} and {strFilterProperty1}";
// Filter the events on the TruckleSoft2 property (this is optional)
if (valueProperty2 != "")
{
string strFilterProperty2 = $"singleValueExtendedProperties/Any(ep: ep/id eq '{SettingsBase.Property_TruckleSoft2}' and ep/value eq '{valueProperty2}')";
// Update the filter
strFilter += $" and {strFilterProperty2}";
}
var oCalendar = await _graphClient.Me.Calendars[oSettings.CalendarID].Request().GetAsync();
string strName = oCalendar.Name;
var oEvents = await _graphClient
.Me
.Calendars[oSettings.CalendarID]
.Events
.Request()
.Filter(strFilter)
.OrderBy("start/DateTime")
.GetAsync();
Program.file.WriteLine($"Calendar {strName}");
int iPage = 0, iTotal = 0, iLoop = 0;
bool bDeleted = false;
do
{
iLoop++;
if (oEvents?.Count > 0)
{
iPage++;
foreach(Event oEvent in oEvents)
{
Program.file.WriteLine($"Loop {iLoop} Page {iPage} Count {oEvents.Count.ToString()} Event Data: {oEvent.Start.DateTime} Id: {oEvent.Id} Subject: {oEvent.Subject}");
await _graphClient.Me.Events[oEvent.Id].Request().DeleteAsync();
bDeleted = true;
}
iTotal += oEvents.Count;
}
if (oEvents.NextPageRequest != null)
oEvents = await oEvents.NextPageRequest.GetAsync();
else
{
oEvents = null;
}
} while (oEvents != null);
Program.file.WriteLine($"Count: {iTotal}" + Environment.NewLine);
return bDeleted;
}
但是,上述内容需要像这样调用:
bool bContinue = await oOutlook.DeleteCalendarEvents(_DataSRR.SettingsSRR, _DataSRR.SettingsSRR.CalendarEntryType, _DataSRR.SettingsSRR.ScheduleType);
while(bContinue)
bContinue = await oOutlook.DeleteCalendarEvents(_DataSRR.SettingsSRR, _DataSRR.SettingsSRR.CalendarEntryType, _DataSRR.SettingsSRR.ScheduleType);
我的日志中的结果:
Calendar Test 5
Loop 1 Page 1 Count 10 Event Data: 09/21/2017 19:15:00 Id: AA== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 09/24/2017 13:00:00 Id: AA== Subject: Weekly Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 09/28/2017 19:15:00 Id: AA== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/01/2017 13:00:00 Id: AA== Subject: Weekly Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/05/2017 19:15:00 Id: AA== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/08/2017 13:00:00 Id: AA== Subject: Weekly Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/12/2017 19:15:00 Id: AA== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/15/2017 13:00:00 Id: AA== Subject: Weekly Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/19/2017 19:15:00 Id: AA== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 10 Event Data: 10/22/2017 13:00:00 Id: AA== Subject: Weekly Meeting Duty Assignments
Count: 10
Calendar Test 5
Loop 1 Page 1 Count 2 Event Data: 10/26/2017 19:15:00 Id: AQ== Subject: Midweek Meeting Duty Assignments
Loop 1 Page 1 Count 2 Event Data: 10/29/2017 13:00:00 Id: AQ== Subject: Weekly Meeting Duty Assignments
Count: 2
Calendar Test 5
Count: 0
如您所见,我必须多次拨打电话。最终它清除了所有商定的事件。但是为什么不能用一次迭代来做到这一点?
答案 0 :(得分:0)
如果我使用$top
选项,我可以调整页面的尺寸,从而减少我需要拨打的电话数量:
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();
这可以进一步简化:
var oEvents = await _graphClient
.Me
.Calendars[oSettings.CalendarID]
.Events
.Request()
.Top(50)
.Filter(strFilter)
.OrderBy("start/DateTime")
.GetAsync();
这resource非常方便。最后我有:
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;
}