ASP Code

Create 'add to calendar' functionality with ASP

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") & "downloads\events"
	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
&#91;/code&#93;

One other thing: if you have any URLs or HTML enchoded characters (like &amp;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 
&#91;code lang="asp"&#93;
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
Share

2 Comments

  1. Our site is hosted on a Unix server with php forms that generate simple emails for the company. The company's in-house server is Microsoft and email client is Outlook. I'm not a techie by any stretch, but can you tell me if it's possible to convert the form-generated emails to Outlook invites?

  2. Hi there. Great code, simple and effective. However, I am having trouble adapting it to work with an asp calendar application that I'm using called vCalendar by UltraApps. It's free and open-source, so maybe you could take a look at their files and help me adapt this code to work with that application. It's available at: http://www.ultraapps.com Thanks. Please email me if you have any questions or results.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image