Google Calendar API in C#

Google recently released an API for Google Calendar. Even though the Google Calendar UI is already pretty slick, developers can now write applications to extend the functionality further. C# and Java versions of the API are available.

For example, you can query your upcoming events in C# like so:

  1. First download & install the libraries. You may need to build them first.
  2. Make a new project and reference GData.dll, GDataExtensions.dll, and CalendarService.dll
  3. Use code kinda like the below πŸ™‚

using System;
using System.IO;
using System.Xml;
using System.Net;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]        static void Main(string[] args)
        {
            string calendarURL = “http://www.google.com/calendar/feeds/default/private/full”;
            string appName = “MyCompany-MyApp-v1.0”;
            string userName = “you@someisp.com”;    // google calendar login
            string password = “somepassword”; // google calendar password

            // create a query object
            // get 10 entries that start between now & the next 10 days
            EventQuery query = new EventQuery();
            query.Uri = new Uri(calendarURL);
            query.NumberToRetrieve = 10;
            query.StartTime = System.DateTime.Now;
            query.EndTime = System.DateTime.Today.AddDays(10);

            // connect to the calendar service
            CalendarService service = new CalendarService(appName);
            service.setUserCredentials(userName, password);

            // execute the query & get the results back
            EventFeed calFeed = service.Query(query);

            // iterate through the results
            foreach (EventEntry feedEntry in calFeed.Entries)
            {
                string eventStartDesc = “n/a”;
                string eventWhereDesc = “n/a”;
                
                if (feedEntry.Times.Count > 0)
                {
                    When eventStart = feedEntry.Times[0];
                    if (eventStart.AllDay)
                        eventStartDesc = “All day”;
                    else if (eventStart.StartTime.Date == System.DateTime.Today.Date)
                        eventStartDesc = “Today at “ + eventStart.StartTime.ToShortTimeString();
                    else
                        eventStartDesc = eventStart.StartTime.ToString();
                }

                if (feedEntry.Locations.Count > 0)
                {
                    Where where = feedEntry.Locations[0];
                    if (where.ValueString != null || where.Label != null)
                        eventWhereDesc = where.Rel + ” “ + where.Label + ” “ + where.ValueString;
                }

                Console.WriteLine(“Event:”);
                Console.WriteLine(“tTitle: “ + feedEntry.Title.Text);
                Console.WriteLine(“tWhen: “ + eventStartDesc);
                Console.WriteLine(“tWhere: “ + eventWhereDesc);

            }
        }
    }
}


As you can see, it’s pretty easy. Note that you should change the username & password information at the top, and make sure you have some upcoming events in your calendar.

You can also download a sample .NET 1.1 project here.

0