问题
i am using EWS Exchange Sync, and in one of my methods, it's saying that i exceeded maximum count of 1000 items that can be deleted in a single request. Can this be solved by instead of deleting everything from the calendar, we only delete from today and onwards, instead of deleting back in time?
The method responsible for the error is here:
public void DeleteAllSafeAppointments(SCDriftConnection conn, ExchangeService service, SAFEAgent agent) {
if(conn == null)
throw new ArgumentNullException("conn");
if(service == null)
throw new ArgumentNullException("service");
if(agent == null)
throw new ArgumentNullException("agent");
try {
var calendar = GetAgentCalendar(service, agent);
service.DeleteItems(GetAllSafeAppointments(calendar).Select(a => a.Id), DeleteMode.HardDelete, SendCancellationsMode.SendToNone, null);
var scheduledActivitiesOnAgent = ScheduledActivity.GetScheduleToExchange(new QueryParameters<ScheduledActivity>(s => s.Initials == agent.Initials && s.ExchangeId != null));
foreach(var schedAct in scheduledActivitiesOnAgent) {
schedAct.ExchangeId = null;
schedAct.Save(conn);
}
ResetAgentSyncState(calendar, agent);
} catch(Exception ex) {
throw new Exception("An error occured while clearing exchange calendar for " + agent.Initials, ex);
}
agent.ExchangeSyncronizationSettings.LastSync = DateTime.Now;
agent.ExchangeSyncronizationSettings.Save(conn);
}
The full error message is here:
> An error occured in Safe-Exchange Sync, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Exceeded maximum count of 1000 items that can be deleted in a single request.
ved Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException)
ved Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
ved Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
ved Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute()
ved Microsoft.Exchange.WebServices.Data.ExchangeService.InternalDeleteItems(IEnumerable
1 itemIds, DeleteMode deleteMode, Nullable1 sendCancellationsMode, Nullable
1 affectedTaskOccurrences, ServiceErrorHandling errorHandling, Boolean suppressReadReceipts)
ved Microsoft.Exchange.WebServices.Data.ExchangeService.DeleteItems(IEnumerable1 itemIds, DeleteMode deleteMode, Nullable
1 sendCancellationsMode, Nullable`1 affectedTaskOccurrences)
ved SafeToExchangeSync.SafeScheduleSyncronizer.DeleteAllSafeAppointments(SCDriftConnection conn, ExchangeService service, SAFEAgent agent)
回答1:
You could just batch the request. I've written a generic batch method for this:
private IEnumerable<IEnumerable<T>> Batch<T>(IEnumerable<T> input, int batchSize)
{
List<T> items = new List<T>();
foreach (var item in input)
{
items.Add(item);
if (items.Count == batchSize)
{
yield return items;
items = new List<T>();
}
}
if (items.Count > 0)
{
yield return items;
}
}
And you can now use it to split your items into batches:
var batches = Batch(GetAllSafeAppointments(calendar).Select(a => a.Id), 1000);
foreach (var batch in batches)
{
service.DeleteItems(batch, DeleteMode.HardDelete, SendCancellationsMode.SendToNone, null);
}
Now a maximum of 1000 will be deleted at once.
回答2:
private IEnumerable<Appointment> GetAllSafeAppointments(Folder calendar) {
ItemView view = new ItemView(512);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(SafeAppointmentFlag, true));
while(true) {
var results = SendExchangeRequest(() => calendar.FindItems(filter, view));
foreach(var r in results.OfType<Appointment>())
yield return r;
if(!results.MoreAvailable)
break;
view.Offset = results.NextPageOffset.Value;
}
}
来源:https://stackoverflow.com/questions/49812301/ews-error-exceeded-maximum-count-of-1000-items-that-can-be-deleted-in-a-single