2017-05-03 64 views
0

我想通過他的電子郵件從我的同事處獲得某個日期範圍內的所有約會。我可以通過outlook訪問他的日曆。我只想知道他是否已經預約了「免費」,「忙碌」或「OOF」。該代碼適用於「完整詳細信息」權限,但不適用於「空閒/忙碌時間,主題,位置」權限級別。通過EWS僅通過「忙/閒時間,主題,位置」許可級別獲得來自同事的約會

我的同事不應該將權限級別更改爲「全部細節」。它應該停留在「忙/閒時間,主題,地點」是這樣的:

enter image description here

我有以下代碼:

private static void GetAllCalendarItems() 
{ 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); 
     service.UseDefaultCredentials = true; 
     service.Url = new Uri("https://example.com/EWS/Exchange.asmx"); 

     FolderId cfolderid = new FolderId(WellKnownFolderName.Calendar, "[email protected]"); 
     Folder TargetFolder = Folder.Bind(service, cfolderid); 
     CalendarFolder calendar = CalendarFolder.Bind(service, cfolderid); 

     CalendarView cView = new CalendarView(DateTime.Now, DateTime.Now.AddDays(30), 5); 

     cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.LegacyFreeBusyStatus); 

     FindItemsResults<Appointment> appointments = null; 
     try 
     { 
      appointments = calendar.FindAppointments(cView); 
     } 
     catch (ServiceResponseException ex) 
     { 
      Debug.WriteLine("Error code: " + ex.ErrorCode); 
      Debug.WriteLine("Error message: " + ex.Message); 
      Debug.WriteLine("Response: " + ex.Response); 
     } 

     foreach (Appointment a in appointments) 
     { 
      Debug.Write("Subject: " + a.Subject.ToString() + "\t\t\t"); 
      Debug.Write("Status: " + a.LegacyFreeBusyStatus.ToString() + "\t\t\t"); 
      Debug.WriteLine(""); 
     } 

} 

此代碼工作正常與我的電子郵件。但不是來自我的同事的。我得到下面的異常在我的try-catch塊:

Exception thrown: 'Microsoft.Exchange.WebServices.Data.ServiceResponseException' in Microsoft.Exchange.WebServices.dll 
Error code: ErrorAccessDenied 
Error message: Access is denied. Check credentials and try again. 
Response: Microsoft.Exchange.WebServices.Data.FindItemResponse`1[Microsoft.Exchange.WebServices.Data.Appointment] 

我有一個給定的權限訪問他的日程表,因爲我可以看到他的Outlook約會,所以我怎麼能獲得與地位的約會從他的同事那裏收到他的電子郵件?

+0

您是否檢查了權限列弗你同事給你的els?或嘗試設置憑據? – Scrobi

+0

是的,我不想用靜態證書來做,它應該與我的AD證書一起工作。我有我的同事的日曆的權限,但它也應該工作如果我沒有任何權利,因爲狀態「自由」,「忙」或「OOF」也可以在Outlook中看到,如果我對他沒有任何權限日曆。 –

+0

找到這個[msdn文章](https://msdn.microsoft.com/en-us/library/office/dn641956(v = exchg.150).aspx)可能有所幫助。 – Scrobi

回答

1

如果這適用於審閱者權限而不是忙/閒設置,我只能假定您需要審閱者權限級別來讀取日曆。

另一種得到什麼忙/閒時間是使用ExchangeService.GetUserAvailability

請參閱以下資源:從櫃面突破上面的鏈接

https://msdn.microsoft.com/en-us/library/office/dn643673(v=exchg.150).aspx https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice.getuseravailability(v=exchg.80).aspx

代碼在未來:

// Create a collection of attendees. 
List<AttendeeInfo> attendees = new List<AttendeeInfo>(); 

attendees.Add(new AttendeeInfo() 
{ 
    SmtpAddress = "[email protected]", 
    AttendeeType = MeetingAttendeeType.Organizer 
}); 

attendees.Add(new AttendeeInfo() 
{ 
    SmtpAddress = "[email protected]", 
    AttendeeType = MeetingAttendeeType.Required 
}); 

// Specify options to request free/busy information and suggested meeting times. 
AvailabilityOptions availabilityOptions = new AvailabilityOptions(); 
availabilityOptions.GoodSuggestionThreshold = 49; 
availabilityOptions.MaximumNonWorkHoursSuggestionsPerDay = 0; 
availabilityOptions.MaximumSuggestionsPerDay = 2; 
// Note that 60 minutes is the default value for MeetingDuration, but setting it explicitly for demonstration purposes. 
availabilityOptions.MeetingDuration = 60; 
availabilityOptions.MinimumSuggestionQuality = SuggestionQuality.Good; 
availabilityOptions.DetailedSuggestionsWindow = new TimeWindow(DateTime.Now.AddDays(1), DateTime.Now.AddDays(2)); 
availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy; 

// Return free/busy information and a set of suggested meeting times. 
// This method results in a GetUserAvailabilityRequest call to EWS. 
GetUserAvailabilityResults results = service.GetUserAvailability(attendees, 
                   availabilityOptions.DetailedSuggestionsWindow, 
                   AvailabilityData.FreeBusyAndSuggestions, 
                   availabilityOptions); 
+0

這個完美的作品!正是我在尋找的東西!如果我設置屬性'availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;''到= availabilityOptions.RequestedFreeBusyView FreeBusyViewType.Detailed;'我也可以拿到課題!完善! –

+0

@PascalAckermann你能查看這個錯誤。 https://stackoverflow.com/questions/49171699/i-have-integrated-free-busy-api-for-outlook。 –

0

您的編譯器不會在此行上引發錯誤嗎?

FolderId cfolderid = new FolderId(WellKnownFolderName.Calendar, "[email protected]"); 

它應該是這樣的:

FolderId cfolderid = new FolderId(WellKnownFolderName.Calendar, new Mailbox("[email protected]")); 

MSDN source

也請介意,僅使用CalendarView的日期範圍,你是在浪費ressouces。嘗試使用SearchFilter代替或另外。

+0

有兩個這樣的行沒有語法錯誤,但仍然在try-catch塊的ServiceResponseException。 –