Google Calendar API: How to add an event to a non-primary calendar?

Hello,

I've made a web service which encapsulates access to the google calendar API, but I can only ever work with the "primary" calendar which has my email address as its' name. I know I'm getting close, but hopefully somebody will be able to look at the code below, and figure out why I can't get it to do exactly what I want...

Specifically, I have a lot of trips to publish, and I'd like to put them on different calendars, like overlays, depending on the properties of the trip, so that, for instance, trips that use a 56 passenger bus are listed on one of the calendars, while others that use smaller vehicles are listed seperately.

The code listed below is a simplified function to post just one static event, and I need to know how to modify it such that it can direct its output to the calendar of choice...

Ideas, anyone?
Private Function CreateEvent2() As String

        Dim entryTitle As String = "", strContent As String = "", strWhere As String = "", _
                                 varFrom As String = "", varTo As String = "", _
                                 recurData As String = ""

        Dim Service As CalendarService = New CalendarService("My-exampleApp-1")
        Service.setUserCredentials(userName, userPassword)

        entryTitle = "(1234) Trip to Funland, USA"
        strContent = "Event body information goes here"
        varFrom = "6/1/2012 6 AM"
        varTo = "6/2/2012 6 pm"
        strWhere = "Funland, USA"

        Dim entry As New EventEntry
        entry.Title.Text = entryTitle 
        entry.Content.Content = strContent

        Dim eventLocation As New Where
        eventLocation.ValueString = strWhere
        entry.Locations.Add(eventLocation)

        Dim eventTime As New [When](varFrom, varTo)
        entry.Times.Add(eventTime)

        Dim postUri As Uri = New Uri("https://www.google.com/calendar/feeds/default/private/full")
        Dim insertedEntry As AtomEntry = Service.Insert(postUri, entry)

        Return insertedEntry.ToString

    End Function

Open in new window

LVL 3
Jon JaquesInformation TechnologistAsked:
Who is Participating?
 
Jon JaquesConnect With a Mentor Information TechnologistAuthor Commented:
Okay, nobody commented on this, but I have now found the solution, and so I will share it here for posterity! :D

First off, there are three main URLs to consider with working with the Calendar API:

There's the link to the current logged in users' Primary Calendar:
https://www.google.com/calendar/feeds/default/private/full
Next there's the URL that gives you the list of All Calendars that the logged in user has in their account; you can use the returned results to obtain the Calendar ID, which you'll need to access individual "non primary" calendars
https://www.google.com/calendar/feeds/default/owncalendars/full
Finally, there's the URL that, when combined with the Calendar ID can be used to access the individual calendars:
https://www.google.com/calendar/feeds/ID_GOES_HERE/private/full

In my original code snippet above, you can see that I was using only the Primary/Private calendar URL, and it was this confusion that made it difficult to figure out why it didn't work. There was also a problem of obtaining the ID in code, so I created a function to do just that, and am including it below:

    <WebMethod()> _
    Public Function GetCalendarID(ByVal strCalName As String) As String
        Dim Service As CalendarService = GetCalendarService()
        Dim query As New FeedQuery
        Dim str As String = ""

        query.Uri = New Uri(feedOwnCalendars)

        Dim calFeed As AtomFeed
        calFeed = Service.Query(query)

        For x As Integer = 0 To calFeed.Entries.Count - 1
            If calFeed.Entries(x).Title.Text = strCalName Then
                str = calFeed.Entries(x).Id.AbsoluteUri.Substring(63)
            End If
        Next

        Return str
    End Function

Open in new window


Also note that the function above makes use of a CalendarService object passed in ByRef; this is so that multiple commands can be sent to the API service within the same connection, thus avoiding being blocked by Google. Here's the function that is now used in my code to produce that CalendarService object without duplicating lots of code:

    Private Function GetCalendarService() As CalendarService
        Dim Service As CalendarService = New CalendarService("KellyTours-exampleApp-1")
        Service.setUserCredentials(userName, userPassword)
        Return Service
    End Function

Open in new window


Simple, huh? LOL
1
 
Jon JaquesInformation TechnologistAuthor Commented:
I have provided a most excellent solution for myself, by myself, with a bunch of help from Google, but most importantly, I answered the question of "why my code wouldn't work" in such a way that I know how to not have a problem with it again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.