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


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

        Dim eventTime As New [When](varFrom, varTo)

        Dim postUri As Uri = New Uri("")
        Dim insertedEntry As AtomEntry = Service.Insert(postUri, entry)

        Return insertedEntry.ToString

    End Function

Open in new window

Jon JaquesInformation TechnologistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jon JaquesInformation 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:
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
Finally, there's the URL that, when combined with the Calendar ID can be used to access the individual calendars:

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

        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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.