Create ‘add to calendar’ functionality with ASP

by admin on July 24, 2009 · 0 comments

When running events and event registrations on a website, it's very beneficial for users to be able to add the even details to their calendars without having to manually enter them. To do this, you need to generate a .vcs file or output to send to the user. You can either generate this and return it in .vcs encoding, but in this code example I'll show you how to actually create a .vcs file, and store it on the server.

This code assume that you've already got the event details, and loaded them into the descriptive variables in the code below:

sub generateCalendarFile( intEvent )   
        dim strDtStart, strTmStart, strDtEnd, strTmEnd, strTitle, strDesc, strAddress, strCity
        dim strProvince, strRegURL, strCECid, strCal, strLinktoReg
       
        strAddress = strAddress & "=0D=0A" & strCity & ", " & strProvince
        strDesc = strDesc & "=0D=0A=0D=0A" & strAddress
        strDesc = cleanString( strDesc ) 'function further down
       
        if isEmpty( strRegURL ) or trim( strRegURL ) = "" then
                strLinktoReg = "default registration URL"
         else
                strLinktoReg = strRegURL
        end if
        strDesc = strDesc & "=0D=0A=0D=0ADon't forget to register!=0D=0A" & replace( strLinkToReg, "=", "=3D" )
       
        strCal = "BEGIN:VCALENDAR" & vbCrLf & _
                         "BEGIN:VEVENT" & vbCrLf & _
                         "SUMMARY;CHARSET=ISO-8859-1;ENCODING=quoted-printable:" & strTitle & vbCrLf & _
                         "DESCRIPTION;CHARSET=ISO-8859-1;ENCODING=quoted-printable:" & strDesc & vbCrLf & _
                         "DTSTART:" & vCalDate( strDtStart, strTmStart ) & vbCrLf & _
                         "DTEND:" & vCalDate( strDtEnd, strTmEnd ) & vbCrLf & _
                         "END:VEVENT" & vbCrLf & _
                         "END:VCALENDAR"
       
        dim vCalPath, vCalFile, strFTPcmd, objFSP, objWrite
        vCalPath = Request.ServerVariables("APPL_PHYSICAL_PATH") & "downloadsevents"
        set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        if Not objFSO.FolderExists( vCalPath ) then call errorMessage( vCalPath & " is an invalid path!" )
       
        vCalFile = intEvent & "_" & intLanguage & ".vcs"
       
        set objWrite = objFSO.OpenTextFile( vCalPath & "" & vCalFile, 2, true )
                objWrite.Write(strCal)
                objWrite.Close()
        set objWrite = nothing
        set objFSO = nothing
       
end sub

For the .vcs file format, the date and time need to be converted to a rather strange format. So here's the function to take the date and time, convert them to and return them in .vcs format:

function vCalDate( strDate, strTime )
        dim arDate
        arDate = split( strDate, "-" )
        strDate = ""
        for j = 0 to 2
                if len( arDate( j ) ) = 1 then arDate( j ) = "0" & arDate( j )
                strDate = strDate & arDate( j )
        next
       
        strTime = replace( strTime, ":", "" )
        strTime = cLng( strTime ) + 40000
        if strTime > 240000 then
                strDate = cStr( cLng( strDate ) + 1     )
                strTime = strTime – 240000
        end if
        strTime = cStr( strTime )
        if len( strTime ) < 6 then
                do until len( strTime ) = 6
                        strTime = "0" & strTime
                loop
        end if
        vCalDate = strDate & "T" & strTime & "Z"
end function

One other thing: if you have any URLs or HTML enchoded characters (like &reg;, etc), they don't play well in the .vcs files. So you need to remove the URLs, and re-encode the symbols in UNICDE format. Here's the function to do that

function cleanString( strToConvert )
'=== remove URLS
'=== takes out HTML friendly chars, and converts to UNICODE equivalent
        dim objRegExp, matches, strChar
        set objRegExp     = New RegExp
        objRegExp.Pattern        = "<a.*?</a>"
        objRegExp.Global         = true
        strToConvert         = objRegExp.Replace( strToConvert, "" )
        set objRegExp      = nothing
       
        set objRegExp     = New RegExp
        objRegExp.Pattern        = "&#.*?;"
        objRegExp.Global         = true
        set matches          = objRegExp.Execute( strToConvert )
       
        for each j in matches
                strChar = j.value
                strChar = cInt( replace( replace( strChar, "&#", "" ), ";", "" ) )
                strChar = chr( strChar )
                strToConvert = replace( strToConvert, j.value, strChar )
        next
       
        set objRegExp      = nothing
        set matches          = nothing
        cleanString         = strToConvert

end function

Previous post:

Next post: