Get appointments from Exchange using EWS

With the Exchange WebService we have a simple way of getting the appointments of your Exchange Mailbox and use the information in your own custom programs.

At first you will have to add the Resource to your Project. You can normally find the .dll here:

C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1
In our example we will use the following model for saving the appointments/dates:

public class Date
    {
        public int Id { get; set; }
        public TimeSpan Duration { get; set; }
        public DateTime End { get; set; }
        public DateTime Start { get; set; }
        public String Location { get; set; }
        public String Subject { get; set; }
        public String Body { get; set; }
    }
Next you should connect to your Exchange WebService using something like this:
public static ExchangeService Connect(string user, string password)
        {
            try
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Credentials = new WebCredentials(user, password);
                service.AutodiscoverUrl(user);
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(“Error: “ + ex.Message);
                return new ExchangeService();
            }
        }
Remember to fit the version to the version your Exchange Server is running. Next you should get the Appointments from your Calendar!
public static Date[] GetAppointments(ExchangeService service, DateTime startDate, DateTime endDate, int NUMAPPTS)
        {
            CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
            CalendarView cView = new CalendarView(startDate, endDate, NUMAPPTS);
            cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Location, AppointmentSchema.Duration);
            FindItemsResults appointments = calendar.FindAppointments(cView);
            List datelist = new List();
            foreach (Appointment a in appointments)
            {
                try
                {
                    datelist.Add(new Date { Id = 11, Location = a.Location, Start = a.Start, End = a.End, Body = “blup”, Subject = a.Subject, Duration = a.Duration });
                }
                catch (Exception ex) { };
            }
            Date[] dates = datelist.ToArray();
            return dates;
            //throw new NotImplementedException();
        }
Download the sample project here