You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EWS Managed API 2.0访问Exchange公共日历相关技术问询

Hey there! Let's break down how to work with public calendars in Exchange 2013 CU13 using the EWS Managed API 2.0—since you're new to Exchange, I'll keep this step-by-step and clear.

1. First: Set Up Your EWS Service Connection

Before doing anything, you need to initialize an ExchangeService object with the right credentials and Exchange version. Make sure you've referenced the EWS Managed API 2.0 assembly first.

using Microsoft.Exchange.WebServices.Data;

// Initialize the service for Exchange 2013
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);

// Set your credentials (use the account with access to public folders)
service.Credentials = new WebCredentials("your_username", "your_password", "your_domain");

// Set the Exchange EWS URL (ask your admin if you don't know this; it's usually like https://your-exchange-server/EWS/Exchange.asmx)
service.Url = new Uri("https://your-exchange-server-url/EWS/Exchange.asmx");

2. Check if the "Pub Cons" Public Calendar Exists

Public calendars are stored as folders with the IPF.Appointment folder class, so we'll filter for that to avoid matching non-calendar folders with the same name.

// Get the root of public folders
Folder publicFolderRoot = Folder.Bind(service, WellKnownFolderName.PublicFoldersRoot);

// Create a view to limit results and fetch necessary properties
FolderView folderView = new FolderView(1); // We only need 1 match max
folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
folderView.PropertySet.Add(FolderSchema.DisplayName);
folderView.PropertySet.Add(FolderSchema.FolderClass);

// Filter for folders named "Pub Cons" that are calendars
SearchFilter calendarFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
    new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Pub Cons"),
    new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF.Appointment"));

// Run the search
FindFoldersResults searchResults = publicFolderRoot.FindFolders(calendarFilter, folderView);

// Check if the calendar exists
bool pubConsExists = searchResults.TotalCount > 0;
if (pubConsExists)
{
    Console.WriteLine("'Pub Cons' calendar found!");
    // Store the calendar folder for later use
    CalendarFolder pubConsCalendar = CalendarFolder.Bind(service, searchResults.Folders[0].Id);
}
else
{
    Console.WriteLine("'Pub Cons' calendar does NOT exist in public folders.");
}

3. Access the "Pub Cons" Calendar's Content

Once you've confirmed the calendar exists, you can fetch appointments using FindAppointments:

if (pubConsExists)
{
    CalendarFolder pubConsCalendar = CalendarFolder.Bind(service, searchResults.Folders[0].Id);
    
    // Define a date range for appointments (adjust as needed)
    DateTime startDate = DateTime.Now.AddDays(-30);
    DateTime endDate = DateTime.Now.AddDays(30);
    CalendarView calendarView = new CalendarView(startDate, endDate);
    
    // Fetch all first-class appointment properties (subject, time, etc.)
    calendarView.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
    
    // Get the appointments
    FindItemsResults<Appointment> appointments = pubConsCalendar.FindAppointments(calendarView);
    
    // Loop through and print details
    foreach (Appointment appt in appointments)
    {
        Console.WriteLine($"Subject: {appt.Subject} | Start: {appt.Start:MM/dd/yyyy hh:mm tt} | End: {appt.End:MM/dd/yyyy hh:mm tt}");
    }
}

4. Traverse All Public Calendars

To list every public calendar (including those in subfolders), use a deep-traversal approach with pagination to avoid performance issues:

// Method to list all public calendars (including subfolders)
void ListAllPublicCalendars(Folder parentFolder)
{
    FolderView folderView = new FolderView(100); // Fetch 100 folders at a time
    folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
    folderView.PropertySet.Add(FolderSchema.DisplayName);
    folderView.Traversal = FolderTraversal.Deep; // Include all nested subfolders
    
    // Filter only calendar folders
    SearchFilter isCalendar = new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF.Appointment");
    FindFoldersResults results = parentFolder.FindFolders(isCalendar, folderView);
    
    // Print found calendars
    foreach (Folder calendarFolder in results.Folders)
    {
        Console.WriteLine($"Public Calendar: {calendarFolder.DisplayName} | ID: {calendarFolder.Id.UniqueId}");
    }
    
    // Handle pagination if there are more results
    while (results.MoreAvailable)
    {
        folderView.Offset += folderView.PageSize;
        results = parentFolder.FindFolders(isCalendar, folderView);
        foreach (Folder calendarFolder in results.Folders)
        {
            Console.WriteLine($"Public Calendar: {calendarFolder.DisplayName} | ID: {calendarFolder.Id.UniqueId}");
        }
    }
}

// Call the method starting from the public folders root
ListAllPublicCalendars(publicFolderRoot);

Important Notes

  • Permissions: Ensure your account has at least read access to the public folders you're trying to access. If you get permission errors, reach out to your Exchange admin to adjust folder permissions.
  • Performance: When traversing all public calendars, pagination (using Offset and PageSize) is crucial to avoid overwhelming the server or your application.
  • Folder Class: Always filter by IPF.Appointment when looking for calendars—this prevents matching non-calendar folders with the same display name.

内容的提问来源于stack exchange,提问作者Burre Ifort

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:25:37