Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / ASP

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

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
26 Oct 2010CPOL1 min read 41.6K   839   22   8
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:

VB.NET
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:

VB.NET
Imports Google.GData.Client 
Imports Google.GData.Calendar 
Imports Google.AccessControl
VB.NET
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.

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer David Weaver Consulting
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAdding events to Google calendar by WP7 Pin
Ankita Pandey29-Mar-12 21:26
Ankita Pandey29-Mar-12 21:26 
QuestionGood to meet you... Pin
matte119-Sep-11 10:30
matte119-Sep-11 10:30 
AnswerRe: Good to meet you... Pin
David Weaver10-Sep-11 3:28
David Weaver10-Sep-11 3:28 
Questionupdating Pin
Member 795654425-Jul-11 6:03
Member 795654425-Jul-11 6:03 
AnswerRe: updating Pin
David Weaver27-Jul-11 6:07
David Weaver27-Jul-11 6:07 
GeneralRe: updating Pin
Member 795654428-Jul-11 4:54
Member 795654428-Jul-11 4:54 
QuestionCant get it to work? Pin
pascalborner5-Jan-11 11:51
pascalborner5-Jan-11 11:51 
AnswerRe: Cant get it to work? Pin
David Weaver6-Jan-11 2:57
David Weaver6-Jan-11 2:57 

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

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