使用EWS Managed API从所有用户删除日历约会

本文关键字:用户 删除 日历 约会 EWS Managed API 使用 | 更新日期: 2023-09-27 18:08:56

我有一个在Exchange Online for Office 365的日历中创建约会的应用程序。我正在使用EWS管理API。

public void CreateAppoitment(string principalName, int taskId) {
  ExchangeService service = createService(principalName);
  ItemView itemView = new ItemView(1000);
  itemView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
  List<Appointment> toCreate = new List<Appointment>();
  // Create the appointment.
  Appointment appointment = new Appointment(service);
  // Set properties on the appointment.
  appointment.Subject = "Test Appointment";
  appointment.Body = "The appointment ...";
  appointment.Start = new DateTime(2014, 6, 18, 9, 0, 0);
  appointment.End = appointment.Start.AddDays(2);
  ExtendedPropertyDefinition epdTaskId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, "TASK_Id", MapiPropertyType.Integer);
  appointment.SetExtendedProperty(epdTaskId, taskId);
  appointment.IsResponseRequested = false;
  toCreate.Add(appointment);
  ServiceResponseCollection<ServiceResponse> createResponse = service.CreateItems(toCreate, WellKnownFolderName.Calendar, MessageDisposition.SaveOnly, SendInvitationsMode.SendToNone);
}

注意我正在设置ExtendedPropertyDefinition "TASK_Id"

我正在使用impersonate在用户的日历中创建约会:

private ExchangeService createService(string principalName) {
  ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
  service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
  service.UseDefaultCredentials = false;
  service.Credentials = new WebCredentials("XXXX", "YYYY");
  service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, principalName);
  return service;
}

然后,给定一个taskId,我想要删除与此taskId的所有约会:

public void DeleteAppointment(string principalName, int appointmentId) {
  ExchangeService service = createService(principalName);
  ItemView itemView = new ItemView(1000);
  itemView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
  ExtendedPropertyDefinition epdTaskId = new ExtendedPropertyDefinition(
    DefaultExtendedPropertySet.Appointment, "TASK_Id", MapiPropertyType.Integer);
  SearchFilter filterOnTaskId = new SearchFilter.IsEqualTo(epdTaskId, appointmentId);
  FindItemsResults<Item> appointments = service.FindItems(WellKnownFolderName.Calendar, filterOnTaskId, itemView);
  List<ItemId> toDelete = appointments.Select(item => item.Id).ToList();
  if (toDelete.Count > 0) {
    ServiceResponseCollection<ServiceResponse> response = service.DeleteItems(
      toDelete, DeleteMode.MoveToDeletedItems, SendCancellationsMode.SendToNone,
      AffectedTaskOccurrence.SpecifiedOccurrenceOnly);
    foreach (ServiceResponse del in response) {
      if (del.Result == ServiceResult.Error) {
        //...
      }
    }
  }
}

但是这种方式service.FindItems()只返回TASK_Id = taskId的principalName的约会,我想要所有用户的约会。有办法做到这一点吗?

使用EWS Managed API从所有用户删除日历约会

Exchange Managed API和Exchange Web Services一次只允许访问一个用户的日历——要么直接使用用户的凭据,要么间接使用模拟让服务帐户访问用户的日历。

一次搜索多个日历需要一种不同的技术。我想到的一个选择是使用Exchange 2013中的eDiscovery操作。虽然它们通常用于查找电子邮件是出于法律原因,但你也可以使用相同的过程。不幸的是,目前eDiscovery的文档非常少,但是您可以在这里看到可用的EWS操作:eDiscovery in EWS in Exchange,并且您可以在ExchangeService对象上找到相应的EWS Managed API方法。