The Protocol
Google Calendar allows client applications to add, update and view scheduled events in the form of Google Data API feeds. Possible use of Google Calendar API is we can create a calendar front-end that uses Google Calendar as a database or backend. Another use is to synchonize it to your existing outlook calendar events.
.NET Client Libray
Before you can reference the Google Data APIs you need to download and install the latest MSI release of the client library from here. Take note that the MSI package contains samples that you can use as templates for your next application.
Developing your first Google Data application
1. From Visual Studio 2005 or 2008, Create a new project.
2. Right-click on the project then select Add Reference.
3. Click on the Browse tab and navigate to Redist directory in the SDK ("C:\Program Files\Google\Google Data API SDK\Redist")
4. Select the DLL files that will be used by your project such as Google.GData.Client.dll, Google.GData.Calendar and etc.
5. Cut and paste the following codes:
On Namespace include,
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;
On your windows or web form, place two textboxes for username and password. Then cut & paste the following code to login to Google Apps:
private bool GoogleLogin()
{
bool lreturn = false;
try
{
CalendarService service = new CalendarService("CalendarSample");
if (userName != null && userName.Length > 0)
{
service.setUserCredentials(this.UserName.Text, this.Password.Text);
}
catch(Exception ex)
{
Console.WriteLine("Error encountered: " + ex.Message);
}
}
To retrieve all calendar events:
// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("CalendarSample");
myService.setUserCredentials(this.UserName.Text, this.Password.Text);
CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.Query(query);
Console.WriteLine("Calendars Entries:\n");
foreach (CalendarEntry entry in resultFeed.Entries)
{
Console.WriteLine(entry.Title.Text + "\n");
}
To create a new calendar entry just follow this code:
CalendarEntry calendar = new CalendarEntry();
calendar.Title.Text = "Sample Event";
calendar.Summary.Text = "This is a sample entry.";
calendar.TimeZone = "Philippines/Manila";
calendar.Hidden = false;
calendar.Color = "#2952A3";
calendar.Location = new Where("", "", "Manila");
Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/owncalendars/full");
CalendarEntry createdCalendar = (CalendarEntry) myService.Insert(postUri, calendar);
Ofcourse you can also update any calendar event:
CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/owncalendars/full");
CalendarFeed resultFeed = myService.Query(query);
//updates the first entry, you can change index
CalendarEntry calendar = (CalendarEntry) resultFeed.Entries[0];
calendar.Title.Text = "Sample Event Again";
calendar.Color = "#A32929";
calendar.Selected = true;
calendar.Update();
To delete an entry, jut call the Delete method.
//this code deletes all entries
CalendarQuery cq = new CalendarQuery();
cq.Uri = new Uri("http://www.google.com/calendar/feeds/default/owncalendars/full");
CalendarFeed rf = myService.Query(query);
foreach (CalendarEntry entry in rf.Entries)
{
Console.WriteLine("Deleting calendar: " + entry.Title.Text + "\n");
try
{
entry.Delete();
}
catch (GDataRequestException)
{
Console.WriteLine("Unable to delete calendar.\n");
}
}
There you have it. These are just basic queries and there are a lot more you can do with Google Data APIs. Visit Google Labs for more details. Good Luck!
5 comments:
Hi
This is good article.
Thanks
I need to publish/share the my calendar and same time to get published/shared URL using google calendar API.
Please provide this information asap.
Thanks in advance
Great tutorial
but am getting continuously this error while creating an event in my google calendar
Execution of request failed: http://www.google.com/calendar/feeds/default/owncalendars/full
kindly tell me the solution.
am stuck here.
Thanks in advance.
@faheem, can you please post the exception details like this one.
Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
Hi ... I am trying storing the event id once i insert a new event.. now i am trying to update the event based on the event id... how do i do it..
Hi, thank you for your post. Could you describe how you would import or sync an outlook calendar via code?
Post a Comment