EWS错误:超过了1000个项目的最大数量,可以在一个请求中将其删除

我正在使用EWS Exchange Sync,在我的一种方法中,这就是说我超出了可以在单个请求中删除的1000个项目的最大数量。是否可以解决此问题,而不是从日历中删除所有内容,而只从今天起删除,而不是及时删除?


导致错误的方法在这里:


        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);

    }


牧羊人nacy
浏览 232回答 2
2回答

LEATH

private IEnumerable<Appointment> GetAllSafeAppointments(Folder calendar) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemView view = new ItemView(512);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.PropertySet = new PropertySet(BasePropertySet.IdOnly);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SearchFilter.IsEqualTo(SafeAppointmentFlag, true));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var results = SendExchangeRequest(() => calendar.FindItems(filter, view));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var r in results.OfType<Appointment>())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return r;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!results.MoreAvailable)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.Offset = results.NextPageOffset.Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

暮色呼如

您可以批量处理请求。我为此编写了一个通用批处理方法:private IEnumerable<IEnumerable<T>> Batch<T>(IEnumerable<T> input, int batchSize){&nbsp; &nbsp; List<T> items = new List<T>();&nbsp; &nbsp; foreach (var item in input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; items.Add(item);&nbsp; &nbsp; &nbsp; &nbsp; if (items.Count == batchSize)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return items;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = new List<T>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (items.Count > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; yield return items;&nbsp; &nbsp; }}现在,您可以使用它将您的物品分成几批:var batches = Batch(GetAllSafeAppointments(calendar).Select(a => a.Id), 1000);foreach (var batch in batches){&nbsp; &nbsp; service.DeleteItems(batch, DeleteMode.HardDelete, SendCancellationsMode.SendToNone, null);}现在最多一次将删除1000个。
打开App,查看更多内容
随时随地看视频慕课网APP