Wriju's BLOG

.NET, Cloud and everything

Exchange Web Service get Calendar information

Now we have our mailboxes and Exchange server serving and storing them in the sever. Using Exchange WebService you can get the information. First build one Windows Application (this case). Add EWS reference from Nuget https://www.nuget.org/packages/Microsoft.Exchange.WebServices/ or you may run this command in Tools > Package Manager Console


After that a very simple code as below

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.UseDefaultCredentials = true; //Windows Auth, you can pass user id and password too
service.Url = new Uri("https://mail.microsoft.com/EWS/Exchange.asmx"); //URL of the service
//Get the next 30 days appointment
FindItemsResults<Appointment> foundAppointments =
        new CalendarView(DateTime.Now, DateTime.Now.AddDays(30)));
foreach (var appo in foundAppointments)
    //Gather information on each Appointment
    //location, datetime, host, attendees, duration
    var location = appo.Location;
    var dt = appo.ICalDateTimeStamp;
    var host = appo.Organizer.Name;
    var duration = (appo.End - appo.Start).Minutes;

    string data = string.Format("Host:{0}\r\nDate:{1}\r\nDuration:{2}\r\nLocation:{3}\r\n", 
                                host, dt, duration, location);

    textBox1.Text += "========\r\n"+data;

Till then enjoy. Its simple.