To access on-premise Exchange user calendar events using C#, you can use the Exchange Web Services (EWS) Managed API. Below are the steps and a sample code snippet to get you started:

  1. Install the EWS Managed API: Download and install the EWS Managed API from Github.
  2. Set up your project: Add a reference to the EWS Managed API DLL (Microsoft.Exchange.WebServices.dll) in your C# project.
  3. Authenticate and connect to Exchange: Use your credentials to connect to the Exchange server.
  4. Access calendar events: Retrieve the calendar events using the EWS API.

Here’s a sample C# code snippet to illustrate how to achieve this:

using System;
using Microsoft.Exchange.WebServices.Data;

namespace ExchangeCalendarAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the service connection to Exchange
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
            
            // Use your credentials here
            service.Credentials = new WebCredentials("your_username", "your_password");

            // Set the URL of the Exchange Web Services (EWS) endpoint
            service.Url = new Uri("https://your_exchange_server/EWS/Exchange.asmx");

            // Specify the user whose calendar you want to access
            Mailbox userMailbox = new Mailbox("user@domain.com");

            // Define the calendar folder to access
            FolderId calendarFolderId = new FolderId(WellKnownFolderName.Calendar, userMailbox);

            // Set the date range for the calendar events you want to retrieve
            DateTime startDate = DateTime.Now;
            DateTime endDate = startDate.AddDays(7);

            // Define the calendar view with the date range
            CalendarView calendarView = new CalendarView(startDate, endDate);

            // Find the calendar items
            FindItemsResults<Appointment> calendarItems = service.FindAppointments(calendarFolderId, calendarView);

            // Loop through the calendar items and display information
            foreach (Appointment appointment in calendarItems.Items)
            {
                Console.WriteLine("Subject: " + appointment.Subject);
                Console.WriteLine("Start: " + appointment.Start);
                Console.WriteLine("End: " + appointment.End);
                Console.WriteLine("Location: " + appointment.Location);
                Console.WriteLine("Body: " + appointment.Body.Text);
                Console.WriteLine("--------------------------------------------");
            }
        }
    }
}

Steps Explained:

  1. Initialize ExchangeService:
    • Set the ExchangeVersion to match your Exchange server version.
    • Provide the credentials to authenticate with the Exchange server.
  2. Set the EWS URL:
    • Specify the URL to the EWS endpoint on your Exchange server.
  3. Specify the User Mailbox:
    • Use the Mailbox class to specify the mailbox you want to access.
  4. Define the Date Range:
    • Use the CalendarView class to define the date range for the calendar events you want to retrieve.
  5. Retrieve Calendar Events:
    • Use the FindAppointments method to retrieve the calendar events within the specified date range.
    • Loop through the retrieved items and display their details.

Prerequisites:

  • Ensure the user account running this code has the necessary permissions to access the target mailbox.
  • Make sure the EWS Managed API is properly installed and referenced in your project.

This example provides a basic framework for accessing calendar events. Depending on your specific requirements, you might need to handle additional scenarios like error handling, paging through large result sets, and dealing with time zone differences.

Access on-premise Exchange user calendar events using C#

Post navigation