Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How To Use the Google Data Protocol to Create Events in the Google Calendar

0.00/5 (No votes)
26 Oct 2010 1  
Template to add, delete or modify Google calendar events

Introduction

This article will show the user how to use the Google Data Protocol to add, edit and delete events from the Google Calendar.

Background

The Google .NET Client Library Developer's Guide examples are written in C#. The author has translated the Google Data API ("GData") queries to VB.NET.

The Google Data API Developer’s Guide: .NET is an excellent source for learning about the Google Calendar API.

Using the Code

You will need to do the following to use the source code below.

Download the Google .NET Client library.

Create a Google email account and a Google calendar.

Set references to the following DLLs (found in the C:\Program Files (x86)\Google\Google Data API SDK\Redist) directory.

  • Google.GData.Client.dll
  • Google.GData.Extensions.dll
  • Google.AccessControl.dll
  • Google.GData.Calendar.dll

Create a new class module named CalendarProperties.vb and add the code below:

Public Enum Request
AddEvent
ChangeEvent
DeleteEvent
End Enum

Public AuthorName As String
Public AuthorEmail As String
Public Title As String
Public TitleUpdate As String
Public Content As String
Public EmailAddress As String
Public EmailPassword As String
Public ServiceName As String
Public CalendarID
Public StartDate As Date
Public EndDate As Date
Public StartTime As String
Public EndTime As String
Public EventLocation As String
Public CalendarPrivateURI As String
Public RequestType As Integer

Create a second class module named CalendarEvents.vb and add the imports to the top of the page. Add the code below to the module:

Imports Google.GData.Client 
Imports Google.GData.Calendar 
Imports Google.AccessControl
Public Shared Function CalendarEvent(ByVal CP As CalendarProperties) As String

 'Set the service 
 'Important: The service name must be in this form exactly or you will get a 401 error
 Dim Service As New Service("Cl", "MyCompany-MyApplication-MyVersion")
 Service.setUserCredentials(CP.EmailAddress, CP.EmailPassword)

 'Set the specific calendar to update using the calendar id
 'the calendar id is on the calendar setting page
 Dim CalendarUri As String = "http://www.google.com/calendar/feeds/" & _
				CP.CalendarID & "/private/full"

 'Add Event
 If CP.RequestType = CP.Request.AddEvent Then

 Try

'Set the location
Dim location As New Where
location.ValueString = CP.EventLocation

'Add location to the entry
Dim entry As New EventEntry
entry.Locations.Add(location)

'Set the dates and times
Dim mydateString As String = CP.StartDate & " " & CP.StartTime
Dim StartDate As Date = Date.Parse(mydateString, _
System.Globalization.CultureInfo.InvariantCulture)

mydateString = CP.EndDate & " " & CP.EndTime
Dim EndDate As Date = Date.Parse(mydateString, _
System.Globalization.CultureInfo.InvariantCulture)

'Add dates to the entry
Dim eventTime As New [When](StartDate, EndDate)
entry.Times.Add(eventTime)

'Set the author
Dim author As New AtomPerson(AtomPersonType.Author)
author.Name = CP.AuthorName
author.Email = CP.AuthorEmail

'Add the author to the entry
entry.Authors.Add(author)

'Add title
entry.Title.Text = CP.Title

'Add content
entry.Content.Content = CP.Content

'Set the Uri 
Dim posturi As New Uri(CalendarUri)

'Send the request
Dim insertedEntry As New AtomEntry
Service.Insert(posturi, entry)


Catch ex As Exception
Return ex.ToString
End Try
Return "OK"
End If

If CP.RequestType = CP.Request.DeleteEvent Then

'Set the query
Dim myquery As New EventQuery(CalendarUri)

Try
'Query the title
myquery.Query = CP.Title

Dim myresults As AtomFeed = Service.Query(myquery)
Dim i As Integer

'Delete the matched titles
For i = 0 To myresults.Entries.Count - 1
 Dim FirstMatch As AtomEntry = myresults.Entries(i)
 FirstMatch.Delete()

Next

Catch ex As Exception
Return ex.ToString
End Try
Return "OK"

End If

If CP.RequestType = CP.Request.ChangeEvent Then
Dim myquery As New EventQuery(CalendarUri)
Try
 myquery.Query = CP.Title

Dim myresults As AtomFeed = Service.Query(myquery)
Dim i As Integer

'Update the matched titles
For i = 0 To myresults.Entries.Count - 1
Dim FirstMatch As AtomEntry = myresults.Entries(i)
FirstMatch.Title.Text = CP.TitleUpdate
FirstMatch.Update()

Next

Catch ex As Exception
Return ex.ToString
End Try
Return "OK"

End If

Return "Error set the request type."

End Function

Next create a web form named CalendarWebForm.aspx. Add the code below to the CalendarWebForm.aspx.vp file that was created.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
	Handles Me.Load
 Dim CP As New CalendarProperties
 Dim CE As New CalendarEvents

 'Set the author 
 CP.AuthorEmail = "Authors email address"
 CP.AuthorName = "Authors name"

 'Set the private address for the calendar. The private address can be found
 'on the calendars settings page next to private address. Click the HTML button
 'and copy the address. This can be used to give read only access to the calendar
 CP.CalendarPrivateURI = "http://PrivateAddres"

 CP.Content = "This is the event content"

 'Your email address used to create the calendar
 CP.EmailAddress = "YourName@gmail.com"

 'Your email password
 CP.EmailPassword = "YourPassword"

 CP.EndDate = "10/25/2010"
 CP.EndTime = "11:00:00 AM"
 CP.StartDate = "10/25/2010"
 CP.StartTime = "8:00:00 AM"
 CP.Title = "Important Meeting"

 'Set the calendar ID the calendar ID can be found on the calendars settings page 
 'next to the calendar address. The calendar ID is used to construct the full URL
 CP.CalendarID = "Your Calendar ID"

 CP.EventLocation = "Your event address"

 'Use this for new events
 CP.RequestType = CP.Request.AddEvent

 'Use this for changes to the title
 'CP.TitleUpdate = "Important Meeting Over"
 'CP.RequestType = CP.Request.ChangeEvent

 'Use this to delete the event. The title must match the event title exactly.
 'CP.RequestType = CP.Request.DeleteEvent

 Dim ReturnString As String = CE.CalendarEvent(CP)
 System.Diagnostics.Debug.Print(ReturnString)

 Response.Redirect(CP.CalendarPrivateURI)

End Sub

If you download the source files, be sure to put the class modules in the app_code directory.

When the project is run, you will be redirected to your Google calendar.

I hope this helps you get started using the Google calendar.

History

  • Version 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here