Thursday, 21 May 2009

For my new 'live' section of my website I decided to use Google Calendar to manage the events and ASP.NET with LINQ to XML to display the events on my site. Getting the right syntax turned out to be a bit tricky, so here's some code that I came up with that should do the trick:

Dim myDT As New DateTime()
myDT = Now

Dim strStartTime As String = ""
Dim strEndTime As String = ""

strEndTime = Left(myDT.AddMonths(12).ToString("s"), 10)
strStartTime = Left(myDT.AddMonths(-1).ToString("s"), 10) 

Dim myAtomNameSpace As XNamespace =  "http://www.w3.org/2005/Atom"
Dim myGDNameSpace As XNamespace = "http://schemas.google.com/g/2005"

Dim MyEventXML As XDocument

MyEventXML = XDocument.Load("http://www.google.com/calendar/feeds/[YOUR CALENDAR UNIQUE ID]@group.calendar.google.com/public/full?start-min=" & strStartTime & "&start-max=" & strEndTime & "")

Dim MyEvents = (From MyEvent In MyEventXML.Descendants(myAtomNameSpace + "entry") _
Order By CDate(MyEvent.Element(myGDNameSpace + "when").Attribute("startTime").Value) _
Select MyEventURL = MyEvent.Element(myAtomNameSpace + "id").Value, _
MyEventTitle = MyEvent.Element(myAtomNameSpace +
"title").Value, _
MyEventDescription = MyEvent.Element(myAtomNameSpace +
"content").Value, _
MyEventStartFormatted =
CDate(MyEvent.Element(myGDNameSpace + "when").Attribute("startTime").Value).ToString("d MMMM yyyy"), _
MyEventVenue = MyEvent.Element(myGDNameSpace +
"where").Attribute("valueString").Value, _
MyEventStart = Left(MyEvent.Element(myGDNameSpace +
"when").Attribute("startTime").Value, 19), _
MyEventEnd = Left(MyEvent.Element(myGDNameSpace +
"when").Attribute("endTime").Value, 19)).ToList

Repeater1.DataSource = MyEvents
Repeater1.DataBind()


blog comments powered by Disqus