Search code examples
c#exchangewebservicesexchange-server-2010

How to retrieve public calendars from EWS?


We are managing the reservations for our car pool via exchange. Every car has a calender on which you can insert an appointment with the car, for when you want to use it.

My task is to retrieve every car's calender and every appointment in it, but I am stuck on how to make the right call via EWS.

My steps are as following:

  • Create an Exchange service
  • Use credentials of service account
  • AutodiscoverUrl()
  • Create CalenderFolder, CalenderView und retrieve appointments.
  • ?

Right now my problem is located at "retrieve appointments" because I can only access my own calender with its WellKnownFolders.

How can I access someone else public calanders and retrieve their appointments?

This is the code I am working with so far: (gathered from http://msdn.microsoft.com/en-us/library/office/dn439786(v=exchg.80).aspx)

ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials(@"domain\svc_account", @"Dummypassword");
service.UseDefaultCredentials = false;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl(@"[email protected]", RedirectionUrlValidationCallback);

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddDays(10);
int num_appts = 10;

CalendarFolder calFolder = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
CalendarView calView = new CalendarView(startTime, endTime, 10);
calView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

FindItemsResults<Appointment> appointments = calFolder.FindAppointments(calView);

foreach (Appointment a in appointments)
{
    //Do stuff later on...
}

Again: This works well for appointments in my calender. I can't find the parts in the MSDN on how to access someone else data.


Solution

  • Have a look at Exchange Impersonation.

    You can have a specific user account impersonate another user account and access their details without the need for their username and password. This should work on resource calendars as well.

    string impName = @"impy";
    string impPassword = @"password";
    string impDomain = @"domain";
    string impEmail = @"[email protected]";
    
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    service.Credentials = new NetworkCredential(impName, impPassword, impDomain);
    service.AutodiscoverUrl(impEmail);
    
    // This will work as if you are that car.
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, @"[email protected]");
    

    More references: http://msdn.microsoft.com/en-us/library/dd633680(v=exchg.80).aspx