2017-04-25 25 views
1

我正在構建一個從Exchange訪問會議的應用程序。我正在使用Microsoft提供的EWS文檔中的代碼。問題是我需要訪問特定的日曆。說,我創建了兩個日曆除了默認的日曆。當我使用此代碼訪問會議時,我只從默認日曆獲取會議。我想要訪問特定日曆中的會議。我怎樣才能做到這一點?接收特定日曆的會議EWS(C#)

感謝您的幫助。

// Initialize values for the start and end times, and the number of appointments to retrieve. 
 
      DateTime startDate = DateTime.Now; 
 
      DateTime endDate = startDate.AddDays(30); 
 
      const int NUM_APPTS = 5; 
 

 
      // Initialize the calendar folder object with only the folder ID. 
 
      CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet()); 
 

 
      // Set the start and end time and number of appointments to retrieve. 
 
      CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS); 
 

 
      // Limit the properties returned to the appointment's subject, start time, and end time. 
 
      cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End); 
 

 
      // Retrieve a collection of appointments by using the calendar view. 
 
      FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView); 
 

 
      Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() + 
 
           " to " + endDate.Date.ToShortDateString() + " are: \n"); 
 
      
 
      foreach (Appointment a in appointments) 
 
      { 
 
       Console.Write("Subject: " + a.Subject.ToString() + " "); 
 
       Console.Write("Start: " + a.Start.ToString() + " "); 
 
       Console.Write("End: " + a.End.ToString()); 
 
       Console.WriteLine(); 
 
      }

回答

0

我覺得你的問題是,你正在使用WellKnownFolderName.Calendar:

CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet()); 

相反,你應該用你所創建的日曆FolderId。要獲取的文件夾(日曆)的ID,你可以使用類似的代碼(在回答中發現:https://stackoverflow.com/a/24133821/1037864

ExtendedPropertyDefinition PR_Folder_Path = new ExtendedPropertyDefinition(26293, MapiPropertyType.String); 
    PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties); 
    psPropSet.Add(PR_Folder_Path); 
    FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root, mbMailboxname); 
    FolderView fvFolderView = new FolderView(1000); 
    fvFolderView.Traversal = FolderTraversal.Deep; 
    fvFolderView.PropertySet = psPropSet; 
    SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF.Appointment"); 
    FindFoldersResults ffoldres = service.FindFolders(rfRootFolderid, sfSearchFilter, fvFolderView); 
    if (ffoldres.Folders.Count > 0) { 
     foreach (Folder fld in ffoldres.Folders) { 
      Console.WriteLine(fld.Id.ToString() + " " + fld.DisplayName); 
     } 
    } 

從fld.Id拿這個值,而不是使用WellKnownFolderName.Calendar。

(我現在沒有可用的Exchange服務器來試用,但我希望你能明白,我的代碼是正確的。)