Click here to Skip to main content
11,481,850 members (65,420 online)
Click here to Skip to main content

Managing Google Calendar Events using .NET

, 15 May 2011 CPOL 31.2K 35
Rate this:
Please Sign up or sign in to vote.
How to create events in Google Calendar programmatically.

Yesterday I posted an article on how to create events in SalesForce programmatically. Now we will do the same thing for Google Calendar. The processes are nearly the same but it’s not as straightforward as the SalesForce one, specially on Event Creation and custom field extensions. One good thing though that Google has is a native .NET API that you can add to your project so you do not need to reference to the Google Calendar Web Service and the DLL handles it for you.

First, you need to download that API and add it in your .NET project. You need to install the Google_Data_API_Setup_{CurrentVerion}.msi and to refer to it, you need to browse to “C:\Program Files (x86)\Google\Google Data API SDK\Redist” on 64 bit machines and “C:\Program Files\Google\Google Data API SDK\Redist” on 32 bit machines by default.

You need to reference four main items for this scenario and it is as follows:

Now include that reference in your code:

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

Then like what we had done with the SalesForce article, we need a method for login and here is the code snippet. Again, I provided a proxy setting in case you need it:

private CalendarService GAuthenticate()
{
    string sGoogleUserName = "username";
    string sGooglePassword = "password";
    Uri oCalendarUri = new Uri("http://www.google.com/calendar/feeds/" + 
                               sGoogleUserName + "/private/full");

    //Initialize Calendar Service
    CalendarService oCalendarService = new CalendarService("CalendarSampleApp");
    oCalendarService.setUserCredentials(sGoogleUserName, sGooglePassword);

    //Use Proxy 
    GDataRequestFactory oRequestFactory = 
        (GDataRequestFactory)oCalendarService.RequestFactory;
    WebProxy oWebProxy = new WebProxy(
         WebRequest.DefaultWebProxy.GetProxy(oCalendarUri));
    oWebProxy.Credentials = CredentialCache.DefaultCredentials;
    oWebProxy.UseDefaultCredentials = true;
    oRequestFactory.Proxy = oWebProxy;

    return oCalendarService;
}

Now let's go to the methods you need; on all of the methods discussed below, you will need GAuthenticate to execute your methods.

1. Creating Calendar Entries in Google programmatically

Now if you noticed, we used four different object types unlike in SalesForce where we used only one object which is the SalesForceService.Event. Here we have to define an Event, Where, When, and ExtendedProperty if you need one. ExtendedProperty is a way of adding properties to objects programmatically, so in this case, we created a property called SynchronizationID which will hold our GUID that you can use with other apps. Hint: I use this for my sync application.

//Set Event Entry 
EventEntry oEventEntry = new EventEntry();
oEventEntry.Title.Text = "Test Calendar Entry From .Net";
oEventEntry.Content.Content = 
  "Hurrah!!! I posted my first Google calendar event through .Net";

//Set Event Location 
Where oEventLocation = new Where();
oEventLocation.ValueString = "New Zealand";
oEventEntry.Locations.Add(oEventLocation);

//Set Event Time
When oEventTime = new When(new DateTime(2011, 5, 31, 9, 0, 0), 
     new DateTime(2011, 5, 31, 9, 0, 0).AddHours(1));
oEventEntry.Times.Add(oEventTime);

//Set Additional Properties
ExtendedProperty oExtendedProperty = new ExtendedProperty();
oExtendedProperty.Name = "SynchronizationID";
oExtendedProperty.Value = Guid.NewGuid().ToString();
oEventEntry.ExtensionElements.Add(oExtendedProperty);

CalendarService oCalendarService = GAuthenticate();

//Prevents This Error
//{"The remote server returned an error: (417) Expectation failed."}
System.Net.ServicePointManager.Expect100Continue = false;

//Save Event
oCalendarService.Insert(oCalendarUri, oEventEntry);

You might also notice the line “System.Net.ServicePointManager.Expect100Continue = false” takes care of the HTTP 417 Error, which is explained here.

2. Searching Calendar Entries in Google programmatically

Searching is also straightforward, just use the EventQuery properties to pass to the CalendarService.Query method. Set EventQuery.Query to the same query you use when you search for the calendar in Google.

You can also filter by date start and end by using the StartDate and EndDate properties.

CalendarService oCalendarService = GAuthenticate();

//Search for Event
EventQuery oEventQuery = new EventQuery(oCalendarUri.ToString());
oEventQuery.Query = "Query String";
oEventQuery.StartDate = new DateTime(2011, 6, 5);
oEventQuery.EndDate = new DateTime(2012, 6, 5);

Google.GData.Calendar.EventFeed oEventFeed = oCalendarService.Query(oEventQuery);

//Delete Related Events
foreach (EventEntry oEventEntry in oEventFeed.Entries)
{
    //Do your stuff here
    string sEventTitle = oEventEntry.Title.ToString();
}

But if you want to search for the ExtendedProperty like the SynchronizationID we created on the event creation on step 1, then you need to use the ExtraParameters property and use extq=[YourCutomProperty:TheValue] to get what you need.

CalendarService oCalendarService = GAuthenticate();

//Search for Event
EventQuery oEventQuery = new EventQuery(oCalendarUri.ToString());
oEventQuery.ExtraParameters = "extq=[SynchronizationID:Your GUID Here]";

Google.GData.Calendar.EventFeed oEventFeed = oCalendarService.Query(oEventQuery);

//Delete Related Events
foreach (EventEntry oEventEntry in oEventFeed.Entries)
{
    //Do your stuff here
    string sEventTitle = oEventEntry.Title.ToString();
}

3. Updating Calendar Entries in Google programmatically

Updating is straightforward, just search for the item and then use the CalendarService.Update method.

CalendarService oCalendarService = GAuthenticate();

//Search for Event
EventQuery oEventQuery = new EventQuery(oCalendarUri.ToString());
oEventQuery.ExtraParameters = "extq=[SynchronizationID:{Your GUID Here}]";

Google.GData.Calendar.EventFeed oEventFeed = oCalendarService.Query(oEventQuery);

//Delete Related Events
foreach (EventEntry oEventEntry in oEventFeed.Entries)
{
    //Update Event
    oEventEntry.Title.Text = "Updated Entry";

    oCalendarService.Update(oEventEntry);
    break;
}

4. Deleting Calendar Entries in Google programatically

Deleting is straightforward as well, just search for the item and then use the CalendarService.Delete method.

CalendarService oCalendarService = GAuthenticate();

//Search for Event
EventQuery oEventQuery = new EventQuery(oCalendarUri.ToString());
oEventQuery.ExtraParameters = "extq=[SynchronizationID:{Your GUID Here}]";

Google.GData.Calendar.EventFeed oEventFeed = oCalendarService.Query(oEventQuery);

//Delete Related Events
foreach (EventEntry oEventEntry in oEventFeed.Entries)
{
    oEventEntry.Delete();
    break;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionMessage Removed Pin
Member 1091415626-Apr-15 23:34
memberMember 1091415626-Apr-15 23:34 
Questionsub : Calendar.exe Exception request failed Pin
p_ashokkumar_be23-Apr-15 1:16
memberp_ashokkumar_be23-Apr-15 1:16 
QuestionCan you search for multiple ID's at once? Pin
sirdaveo1-Feb-14 19:00
membersirdaveo1-Feb-14 19:00 
GeneralMy vote of 5 Pin
shielawxy4-Oct-12 9:49
membershielawxy4-Oct-12 9:49 
QuestionA specific calendar Pin
Paul Rohith4-Sep-12 3:36
memberPaul Rohith4-Sep-12 3:36 
GeneralUri needs to be https Pin
Member 30870848-Jul-12 23:41
memberMember 30870848-Jul-12 23:41 
GeneralRe: Uri needs to be https Pin
Paul Rohith4-Sep-12 3:49
memberPaul Rohith4-Sep-12 3:49 
QuestionHow do I clear this error when trying to use this program? Pin
John Sterling Robertson23-Mar-12 4:59
memberJohn Sterling Robertson23-Mar-12 4:59 
QuestionRe: How do I clear this error when trying to use this program? Pin
Raymund Macaalay25-Mar-12 13:12
memberRaymund Macaalay25-Mar-12 13:12 
AnswerRe: How do I clear this error when trying to use this program? Pin
John Sterling Robertson26-Mar-12 3:46
memberJohn Sterling Robertson26-Mar-12 3:46 
GeneralRe: How do I clear this error when trying to use this program? Pin
Raymund Macaalay28-Mar-12 10:00
memberRaymund Macaalay28-Mar-12 10:00 
QuestionMy vote of 5! Pin
MaulikDusara7-Nov-11 18:32
memberMaulikDusara7-Nov-11 18:32 
Questionauthentication Issue Pin
Azmat ullah8-Aug-11 4:44
memberAzmat ullah8-Aug-11 4:44 
AnswerRe: authentication Issue Pin
Raymund Macaalay8-Aug-11 11:05
memberRaymund Macaalay8-Aug-11 11:05 
GeneralRe: authentication Issue Pin
Azmat ullah16-Aug-11 22:22
memberAzmat ullah16-Aug-11 22:22 
AnswerRe: authentication Issue Pin
jackie12312-Sep-11 9:33
memberjackie12312-Sep-11 9:33 
AnswerRe: authentication Issue Pin
CoreyJG15-Dec-11 11:31
memberCoreyJG15-Dec-11 11:31 
GeneralRe: authentication Issue Pin
deepu sehrawat25-Sep-12 21:32
memberdeepu sehrawat25-Sep-12 21:32 
AnswerAuthentication Error 405 Pin
Alhoot200419-Feb-13 15:49
memberAlhoot200419-Feb-13 15:49 
GeneralMy vote of 5 Pin
yrsk.aravind27-Jul-11 10:27
memberyrsk.aravind27-Jul-11 10:27 
GeneralMy vote of 5 Pin
krb4220-May-11 4:45
memberkrb4220-May-11 4:45 
GeneralMy vote of 5 Pin
saxenaabhi615-May-11 20:38
membersaxenaabhi615-May-11 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 May 2011
Article Copyright 2011 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid