The Google calendar API is at its root very powerful, but you have not worked with XML/HTTP, cookies or authentication in the past learning the raw API can be  daunting. Fortunately Google provides a free .NET library, so you can interact with a user’s Google Calendar using intuitive .NET classes and methods.

To get started, download the .NET client API from here:

http://code.google.com/apis/gdata/client-cs.html

Add references to the Google libraries in your project, and use or import these in your code

using Google.GData.Client;
using Google.GData.Calendar;
using Google.GData.Extensions;

The library provides a “Service” class to handle connecting to and authenticating with Google:

Service service = new Service("cl", "companyName-appName-1");
service.setUserCredentials("useremail@userdomain.com", "userpassword");

You then execute a query on the service. The query will return a list of AtomEntry objects, which are handy wrappers for standard RSS Atom entries. For example, this returns a list of all items in the user’s calendar:

FeedQuery query = new FeedQuery();
query.Uri = new 
            Uri("http://www.google.com/calendar/feeds/useremail@userdomain.com/private/full");
AtomFeed calFeed = service.Query(query);
foreach (AtomEntry entry in calFeed.Entries)
{
    listBox1.Items.Add(entry.Title.Text);
}

There are similar methods for adding, updating, or deleting calendar items. For example:

AtomPerson author = new AtomPerson(AtomPersonType.Author);
author.Email = "useremail@userdomain.com";
entry.Authors.Add(author);
entry.Title.Text = "Lunch at Bully's";
entry.Content.Content = "Eat lunch with Fred";
When eventTimes = new When();
eventTimes.StartTime = DateTime.Parse("7/13/2009 12:00:00 PM");
eventTimes.EndTime = DateTime.Parse("7/13/2009 1:00:00 PM");
entry.Times.Add(eventTimes);
Uri postUri = new Uri("http://www.google.com/calendar/feeds/useremail@userdomain.com/private/full");
// Send the request and receive the response:
AtomEntry insertedEntry = service.Insert(postUri, entry);

Google provides extensive documentation and sample code, in a dazzling variety of languages. Even Objective-C for you Mac developers.

As with any scenario where the data can be updated from multiple locations, your application will have to implement synchonization logic. The items you upload one day could be deleted from the web or from the user’s iPhone before your next sync. But fortunately the sometimes daunting task of communicating with the remote service is trivial in this case.

About these ads