2014-06-17 48 views
1

我有一個應用程序在Office 365的Exchange Online的日曆中創建約會。我正在使用EWS託管API。使用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; 
} 

然後,給定一個任務id,我想刪除所有與此任務的約會ID:

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的約會,我想約會所有用戶。有沒有辦法去做到這一點?

回答

0

Exchange託管API和Exchange Web服務一次只允許訪問一個用戶的日曆 - 既可以直接使用用戶的憑據,也可以間接使用模擬爲服務帳戶授予訪問用戶日曆的權​​限。

要一次搜索多個日曆需要不同的技術。想到的一個選擇是使用Exchange 2013中的電子數據展示操作。雖然它們通常用於查找電子郵件是出於法律原因,但您也許可以使用相同的過程。不幸的是,eDiscovery的文檔現在非常稀少,但您可以看到EWS操作在這裏可用:eDiscovery in EWS in Exchange,您可以在ExchangeService對象上找到相應的EWS託管API方法。

相關問題