Over the past few months, I have been going round-and-round trying to figure out how to link to individual events in a Google Calendar, allowing visitors to add the event to their own calendars. Google does a nice job of providing functionality to add an event to your own Google calendar, but they don’t seem to offer any functionality to add an event to other calendars.
Google does, however, provide a link to an iCal file for each calendar’s feed; which allows you to add the calendar itself to your own calendar program (Outlook, etc.). The problem with that, obviously, is that, instead of adding a single event (maybe a concert or conference you want to attend, a public meeting, etc.), it adds all of the events from that calendar.
A Possible Solution
After a lot of digging, head-desking and testing, I think I might have found a solution, though. When you read an XML/Atom feed from a Google calendar, there is an <id> element for each event listed in the feed. That <id> element is actually a URL/permalink for the event, and leads to an XML file with just that event’s data in it.
Once I discovered that, I decided to do some further testing. In the Google calendar API reference, it mentions that you can use a URL structure like the following to get the feed for an individual event:
If you compare that to the URL structure that Google provides for an individual calendar’s feed (the whole calendar):
then compare that to the URL structure that Google provides for an individual calendar’s iCal feed (again, the whole calendar):
you might start to notice some similarities.
The Event’s iCal URL Structure
It turns out (I still have some testing to do, but my preliminary testing indicates that it works), if you use a URL structure like the following, you should be able to grab an iCal file (.ics) for an individual event:
So, how do you do that? Well, simply grab the value of the <id> element from the calendar feed, then replace the string
/feeds/ with the string
/ical/ and append the file extension
.ics to the end.
A Potential Issue
One last potential issue arises when you do that, though. Outlook (and, presumably, other calendar programs) still interprets the downloaded .ics file as a calendar feed rather than an individual event/appointment. When you open the file in Outlook, rather than displaying the normal “Appointment” edit dialog, it actually opens it as an entirely new calendar.
That can be corrected, though, by cracking open the downloaded .ics file and removing the line that begins with:
Once that line is no longer in the .ics file, Outlook will open the file as an individual event/appointment for you to save to your calendar, rather than opening it as a whole new calendar.
Obviously, you wouldn’t want to require your users to do that themselves, so you’d probably want to write your own script that retrieves the file from Google’s server (possibly caching it for a while if you don’t edit events very often), locates the line mentioned above, removes it, saves the file in a temporary location on your server and then prompts the user to download it.
As I mentioned, I still have some testing to do with this, but I’d love feedback on the concept. Have you tried this before? Am I missing anything obvious?