Skip to main content

Adding Calendar Events to Google Calendar in C#

I’ve replaced a custom event page for the Thistle Society with a list extracted dynamically from a Google Calendar. Following on from the previous article, Loading XML in C# with XML Serialisation, I needed to upload the events into the Google Calendar from my C# application (Event Manager).

To do this, first spend hours scouring the internet for the information on how to do it… Ah, you have and that brought you here, did it? Good.

Create the Calendar

If you haven’t got one already, you will need to create a Google Calendar account. To do this, go to the Google home page and look at the menu at the top. Under the “more” menu item you will find a list of menu items including “Calendar”. Click this and you are taken to a page where you can either sign in if you already have an account, or “Create an account”.

Click the button to create an account. You will be presented with a simple form to fill in which is not too scary.

First, they want you to provide your email address to be used as your Google Calendar User Id.  For the Thistle Society calendar I created a specific email address on the site URL which forwards to me and used that to create the account. I also had to create a password which I will show below as “password”. (Yet another password to remember…).

Once created it’s worth having a look through the various features – you can keep it as a private calendar (only you can see it) add email addresses of other people who can access it (full access or read only – it sends them an email to tell them) or even make it public.

You can add in some public calendars or link in your own or family/friends private accounts (they need to give you access). I have added the Thistle calendar into my own calendar and also added weather, UK public holidays and such.

You can also make it synchronise with Outlook and some other desktop calendar applications.

Google .NET APIs

Now to create an application to upload the events to the calendar.  I’ve expanded the Event Manager application I described in the previous article.

Well, you will need to get a copy of the Google .NET Client APIs – from here Click on the Download link and select Google Data API Setup ( or newer version if available. This is also a good place to find the API docs and related documentation. I found useful.

This installed the Google Data .NET assemblies in C:Program FilesGoogleGoogle Data API SDKRedist. Your installation may put them somewhere else – just have a look around if you can’t find it.

Next, you need to add the required assemblies to your project as References – in SharpDevelop right click on the References folder in your project and select Add Reference. In the “.NET Assembly Browser” click the Browse button and select each of the required assemblies from the above folder. Its similar if you are using Visual Studio or another .NET IDE.

Based on trial and error when I added the various bits of code from the examples (clearly the best approach…) I determined that these assemblies were required: –

  • Google.GData.AccessControl (this one wasn’t in the examples I saw)
  • Google.GData.Calendar
  • Google.GData.Client
  • Google.GData.Extensions

Writing the Application Code

To encapsulate the Google Calendar code I created a GoogleCalendarUtils class with everything necessary inside.

First, we need use some of the referenced libraries, and then begin the class – the namespace is Event_Manager because that was the name of the application:

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

namespace Event_Manager
    public class GoogleCalendarUtils


I’ve defined some key values for the calendar interface:

  • Calendar URI – this is the address of the calendar feed we are going to upload to. You only need to change the email address part as yours won’t be
  • Application Name – a name for your application – the API docs say it should take the form companyNameapplicationNameversionID.
  • User Name – your Google Calendar login name which will be the email address you used to create the calendar – and the one in the URI above.
  • Password – your password that goes with the user name above.

Now, it is possible to enable others with calendar logins to make changes to your calendar and vice versa. Therefore, I assume it is acceptable to use different User Name to the Calendar name… but I haven’t tried it.

        private static Uri calendarUri = new Uri("");
        private static string appName = "ThistleSoc-EventManager-0.0.1";
        private static string userName = " ";
        private static string password = "password";


Note: the calendar feed can be accessed using either ATOM or JSON-C formats – I’ve used Atom format in this document – not that it matters because the Google APIs take care of all of it.

In the constructor, I create a Google.GData.Calendar.CalendarService for the application and initialise it with the username and password. We don’t need the URI just yet.

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


I added a method to add a single event – this will be called in a loop by the application to add each event. It takes as a parameter an instance of the ThistleEvent class introduced in the last post.

        public void InsertEntry( ThistleEvent thistleEvent )
            EventEntry entry = new EventEntry();

            entry.Title.Text = XmlUtils.ToString( thistleEvent.Title );
            string htmlDescription = XmlUtils.ToString( thistleEvent.Description );
            if( htmlDescription!= null && htmlDescription.Length > 0 )
                entry.Content.Type = "html";
                entry.Content.Content = htmlDescription;


In it, I create an Google.GData.Calendar.EventEntry and set the Title and Description – using a utility method also introduced in the last post, XmlUtils.ToString().

If the event location is specified this can be added as an instance of the Google.GData. Extensions.Where class. This is then added to the Locations collection of the entry.

            if( thistleEvent.Location != null && thistleEvent.Location.Length > 0 )
                Where eventLocation = new Where();
                eventLocation.ValueString = thistleEvent.Location;
                entry.Locations.Add( eventLocation );


The event dates and times are a bit fiddly – the code below caters for events that just have a start date, start date and time, start and end dates, and start and end dates and times. In the ThistleEvent class, the dates are DateTime classes (wrapped in a Nullable so they can be empty), but the times are Strings and are empty if no time is specified.

In the following code I create a new Google.GData.Extensions.When instance and fill in the values.

            string text = thistleEvent.EventDate.Value.ToShortDateString() + " " + thistleEvent.EventTime;
            DateTime start = DateTime.Parse( text.Trim() );

            When eventTime = new When();
            eventTime.StartTime = start;
            if( thistleEvent.EventTimeTo != null && thistleEvent.EventTimeTo.Length > 0 )
                if( thistleEvent.EventDateTo.HasValue )
                    text = thistleEvent.EventDateTo.Value.ToShortDateString() + " " + thistleEvent.EventTimeTo;
                    text = thistleEvent.EventDate.Value.ToShortDateString() + " " + thistleEvent.EventTimeTo;
                DateTime endTime = DateTime.Parse( text.Trim() );
                eventTime.EndTime = endTime;
            else if( thistleEvent.EventDateTo.HasValue )
                eventTime.EndTime = thistleEvent.EventDateTo.Value;



For All Day events (i.e. no time specified) then we set a flag on the When instance.

            if( (thistleEvent.EventTime == null || thistleEvent.EventTime.Length == 0) &&
               (thistleEvent.EventTimeTo == null || thistleEvent.EventTimeTo.Length == 0) )
                eventTime.AllDay = true;


The following code adds the author (me) to each event.

            AtomPerson author = new AtomPerson( AtomPersonType.Author );
            author.Name = "Simon Williams";
            author.Email = "";
            entry.Authors.Add( author );


Now, with all the event details added, it is added to the Google Calendar – notice that this is where the Calendar URI is specified.

            AtomEntry insertedEntry = service.Insert(calendarUri, entry);


Looping through all the events adds them to the calendar one by one.  The following code does this for all the Thistle Events (which are grouped by Month, remember):

    GoogleCalendarUtils utils = new GoogleCalendarUtils();
    ArrayList months = /* the list of months*/;

    //    Update the content window.
    foreach( ThistleEventMonth month in months )
        foreach( ThistleEvent thistleEvent in month.ThistleEvents )
            utils.InsertEntry( thistleEvent );


Once added, you can check them in the Google Calendar to see they have appeared correctly. 


So that’s the events loaded to the calendar. I tried it a few times (limiting the number) and deleted them (using the Google Calendar list view) if they hadn’t loaded as I wanted them. Eventually, I was happy with them.

In my next article, I describe one way to retrieve the events from Google Calendar using PHP.

13 comments on “Adding Calendar Events to Google Calendar in C#”

  1. simon says:

    I’ve attached some of the code to the previous posting.

  2. Chad Rosenthal says:

    Thanks for the post. It worked like a charm and was definitely a lot easier to read then the Google API Docs.

  3. prateek says:

    how can i see the last post

  4. simon says:

    If you mean the post that follows this one and tells you how I embedded the content into the Thistle Society Events page…I’ve not written it yet. I’ll get it done this week and add a link at the end of this one.

  5. prateek says:

    thanks for reply,but i was talking about last post with thistle class,but i got it

  6. Alex says:

    Hi. What about reminders (notify) ?

    1. simon says:

      At a guess, the code below should do it:

      Reminder reminder = new Reminder();
      reminder.Minutes = 15;
      reminder.Method = Reminder.ReminderMethod.alert;
      eventTime.Reminders.Add( reminder );

      Remember eventTime is an instance of a When class.

      Also, see

  7. We have says:

    Made up my mind to just stop by as well as tell you how a lot I like this webpage 🙂 The articles here are always interesting at the very least.

  8. sudheer says:

    i want know hoe to call gcal properties in .net

  9. Waqas says:

    Hello..It was very use full for me. Thank you for sharing your work. Keep it Up !
    But I have one question.
    If i want to some add guest and send them email then what should I do ?

  10. vikas says:

    Please provide the code for above article as I am also head banging against the same functionality.

    Thanks a lot.

  11. Vinay says:


    Is there any update with v3 of Google API?

    I also need to access calendar events of all participants, I want to invite in a meeting; similar to Microsoft Outlook Calendar view, for all participants.

    And, I don’t want any acknowledgement or acceptance of calendar access.

    Please suggest.

Leave a Reply

Your email address will not be published. Required fields are marked *