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
Dim Service As New Service("Cl", "MyCompany-MyApplication-MyVersion")
Service.setUserCredentials(CP.EmailAddress, CP.EmailPassword)
Dim CalendarUri As String = "http://www.google.com/calendar/feeds/" & _
CP.CalendarID & "/private/full"
If CP.RequestType = CP.Request.AddEvent Then
Try
Dim location As New Where
location.ValueString = CP.EventLocation
Dim entry As New EventEntry
entry.Locations.Add(location)
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)
Dim eventTime As New [When](StartDate, EndDate)
entry.Times.Add(eventTime)
Dim author As New AtomPerson(AtomPersonType.Author)
author.Name = CP.AuthorName
author.Email = CP.AuthorEmail
entry.Authors.Add(author)
entry.Title.Text = CP.Title
entry.Content.Content = CP.Content
Dim posturi As New Uri(CalendarUri)
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
Dim myquery As New EventQuery(CalendarUri)
Try
myquery.Query = CP.Title
Dim myresults As AtomFeed = Service.Query(myquery)
Dim i As Integer
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
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
CP.AuthorEmail = "Authors email address"
CP.AuthorName = "Authors name"
CP.CalendarPrivateURI = "http://PrivateAddres"
CP.Content = "This is the event content"
CP.EmailAddress = "YourName@gmail.com"
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"
CP.CalendarID = "Your Calendar ID"
CP.EventLocation = "Your event address"
CP.RequestType = CP.Request.AddEvent
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