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

时间:2014-06-17 17:13:05

标签: c# exchangewebservices office365 appointment

我有一个应用程序,可以在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”

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

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()只返回principalName与TASK_Id = taskId的约会,我想要约会所有用户。有办法吗?

1 个答案:

答案 0 :(得分:0)

Exchange托管API和Exchange Web服务一次只能访问一个用户的日历 - 直接使用用户凭据或通过模拟间接授予服务帐户访问用户日历的权​​限。

要一次搜索多个日历,需要使用其他技术。想到的一个选项是在Exchange 2013中使用eDiscovery操作。虽然它们通常用于查找出于法律原因的电子邮件,但您可以使用相同的过程。遗憾的是,eDiscovery的文档现在非常稀疏,但您可以在此处查看可用的EWS操作:eDiscovery in EWS in Exchange,您可以在ExchangeService对象上找到相应的EWS托管API方法。 / p>