Write a C# Application to Retrieve Calendar Items from Microsoft Exchange 2016 Using EWS

Lindsay Leeds
4 min readApr 28, 2022

Step By Step Directions Using Visual Studio 2022

Introduction

I wanted to do the simplest thing I could to talk to EWS on Exchange 2016 Server (on premises) using C#. I’m going to show you step by step how I was able to pull a list of calendar appointments from calendar.

Creating a Project

First I launched Visual Studio 2022 and chose “Create a new project” from the opening menu as shown below.

For Project Type I searched for “console” and chose “Console App (.NET Framework)”. Make sure it is .NET Framework and not just “Console App”.

When naming your project go ahead and confirm you .NET Framework is 4.7.2. I would expect older versions of 4.X may work, but I cannot confirm that.

We now a very simple console app with no code.

Adding the Exchange Reference to the Project

Next we will get the NuGet package we need to be able to use EWS services from C#. I used the GUI NuGet package manager in Visual Studio.

On the upper right hand corner I chose “All” for Package source.

I search for “microsoft.webservices.managed.api” and used the newer “Exchange.WebServices.Managed.Api” package. This seems to be the newest version of the library.

Click on it to highlight it.

Here is the project info. Note the 2019 date for “Date published”.

Next I checked the box next to my project and click “Install” to add it as a reference to the project.

I can confirm success in the Output window.

In the top of the code, add a Using statement for our newly added reference:

using Microsoft.Exchange.WebServices.Data;

Now I replace the Main() function with the following code. When run it should show you 10 appointments within the next 30 days on the console screen.

Make sure you change the username and password and the EWS url for your domain.

Here is the full code.

static void Main(string[] args)
{
var service = new ExchangeService(ExchangeVersion.Exchange2016);

service.Credentials = new WebCredentials("myuserid", "mypassword");
service.Url = new Uri("https://clientaccess.mycorp.com/EWS/Exchange.asmx");

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(30);
const int NUM_APPTS = 10;
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.
DateTime startTime = DateTime.Now;
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
DateTime endTime = DateTime.Now;
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();
}
var timeToRun = endTime - startTime;
Console.Write($"It took {timeToRun.TotalSeconds.ToString()} seconds to run...");
}

If you hit Ctrl->F5 to run the program it should pause to show you the output before closing the command window.

--

--

Lindsay Leeds

I am an IT guy by trade, with interests in investing and personal finance.