Integrating ASP.NET and Google Calendar
Tuesday 24 June 2008We recently decided to store annual leave requests for epiGenesys staff in a Google Calendar. This makes it easy to share the information, and to visualise it against events in our other calendars. I decided this evening that it would also be useful to feed this information directly from the calendar into Project MART (our internal Project Management And Reporting Tool, custom built using ASP.NET).
Fortunately, Google have made the process of integrating their services with ASP.NET applications remarkably straightforward. Their developer’s guide outlines the various Google Calendar API functions available, and provides a link to the required DLLs to access the API from an ASP.NET application. Essentially all that’s needed is to create a service with appropriate credentials, pass it a query pointing at the correct calendar, and then parse the returned results feed (a collection of calendar entries) to extract the desired information.
Filtering the results by start and end dates is easily achieved by adding parameters to the query. As we record the initials of the staff member in each entry on the calendar, we can also filter by person by adding a text search parameter.
As an example I have included the following function, which sums the duration in days of all events on the leave requests calendar for a particular person:
public int CountLeaveDaysRequested()
{
CalendarService myService = new CalendarService("epigenesys-project-mart");
myService.setUserCredentials("username", "password");
EventQuery myQuery = new EventQuery("http://google_calendar_url");
myQuery.StartTime = new DateTime(2007, 12, 1);
myQuery.Query = FirstName.Substring(0, 1) + LastName.Substring(0, 1);
myQuery.UseSSL = true;
EventFeed myResultsFeed = myService.Query(myQuery);
int count = 0;
foreach (Google.GData.Calendar.EventEntry entry in myResultsFeed.Entries)
{
foreach (When when in entry.Times)
{
count += when.EndTime.Subtract(when.StartTime).Days;
}
}
return count;
}
This function is used by Project MART to calculate some of the most critical information in the business - how many days of leave we all have left to use!
