Posts Tagged: ‘WebDevelopment’

Dynamically Create vCard Download from LotusScript

14. Juli 2010 Posted by Jens Polster

Reading Matt's entry about the content-disposition http header reminded me of a LotusScript agent I wrote recently which also uses this technique.

I have created a nice looking office locator for one of our clients which uses the Google Maps API and Dojo Toolkit to display the results. For every office you can also download a vCard which is dynamically created from a company record stored in a Notes document*. The vCard is created by an agent which is called with the UNID of the company doc in a URL parameter. The agent created the vCard text and returns it as an attachment with a custom file name using the aforementioned content-disposition http header.

Please read on for the agent code.

Of course you might have to change the field names in strOutPutFieldsList etc. I have used Jake Howlett's ExplodeQueryString routine with some minor modifications for simplicity and Julian Robichaux' excellent OpenLog for error handling.

Call the agent with an URL like http://hostname/db.nsf/agGetVCard?Open&id=the_unid_of_a_company_doc.

See download link at the bottom of the page.

'/** ' * Agent (Get vCard) ' * URL-triggered agent which returns a vCard for the company specified by the ID parameter (this is the UNID) ' * @author Jens Polster (jens dot polster at domblog dot de) ' */ Option Public Option Declare Use "OpenLogFunctions" Sub Initialize() On Error GoTo ErrorHandler Dim sess As New NotesSession Dim dbCurrent As NotesDatabase Dim docContext As NotesDocument Dim strCompanyID As String Dim docCompany As NotesDocument Dim strResult As String Dim strOutputFieldsList List As String Dim strEvaluateMacro As String Dim varEvaluateResult As Variant Dim strNL As String Dim AgentArgs List As String Set dbCurrent = sess.Currentdatabase Set docContext = sess.Documentcontext strNL = Chr$(13) & Chr$(10) ' put URL parameters in a list Call ExplodeQueryString (docContext.Query_String_Decoded(0), AgentArgs) If IsElement( AgentArgs ("id") ) Then strCompanyID = AgentArgs ("id") ' get the company doc's UNID from the list End if If Len(strCompanyID) = 0 Then Error 1001, |No id specified| End If Set docCompany = dbCurrent.GetdocumentbyUNID(strCompanyID) If docCompany Is Nothing Then Error 1003, |Company document with ID | & strCompanyID & | not found.| End If ' create a list of field names and @formula expressions to compute the values strOutputFieldsList("N")=|@ReplaceSubstring(CompanyName; "<br>"; " ")| strOutputFieldsList("FN")=|@ReplaceSubstring(CompanyName; "<br>"; " ")| strOutputFieldsList("ADR;INTL;POSTAL;WORK")=|@Implode("":"":OfficeStreetAddress:OfficeCity:| &_ |"":OfficeZIP:OfficeCountry; ";")| strOutputFieldsList("TEL;PREF;WORK")=|CompanyPhoneNumber| strOutputFieldsList("TEL;FAX")=|OfficeFaxPhoneNumber| strOutputFieldsList("EMAIL")=|CompanyMailAddress| strOutputFieldsList("GEO")= |@ReplaceSubstring(@Text(Latitude); ","; ".") + "," + | & _ |@ReplaceSubstring(@Text(Longitude); ","; ".")| strOutputFieldsList("ORG")=|@ReplaceSubstring(CompanyName; "<br>"; " ")| strOutputFieldsList("URL;WORK")= |CompanyWebsite| strOutputFieldsList("Version")= |"2.1"| strOutputFieldsList("PHOTO")=|OfficePictureURL| strResult = |BEGIN:VCARD| ForAll i In strOutputFieldsList varEvaluateResult = Evaluate(i, docCompany) strResult = strResult & strNL & ListTag(i) & ": " & varEvaluateResult(0) End ForAll strResult = strResult & strNL & |END:VCARD| DefaultExit: strEvaluateMacro = |@ReplaceSubstring(@Ascii(CompanyName); "<br>":" "; " ":"_")| varEvaluateResult = Evaluate(strEvaluateMacro, docCompany) ' set content-type to prevent Domino from creating HTML markup Print |content-type: text/x-vCard; charset=iso-8859-1| ' make the browser handle the data as an attachment Print |content-disposition: attachment; filename="| & varEvaluateResult(0) & |.vcf"| ' the next line is necessary to avoid that "BEGIN:VCARD" is being interpreted as a http header Print Print strResult Exit Sub ErrorHandler: strResult = || Call LogError() Resume DefaultExit End Sub '/** ' * Sub ExplodeQueryString ' * Create a list of all query_string parameters. ' * Slightly modified version from Jake Howlett's blog entry at ' * @param QueryString URL Query_String ' * @param AgentArgs List in which the URL parameters are returned ' */ Private Sub ExplodeQueryString (QueryString As String, AgentArgs List As String) On Error GoTo ErrorHandler Dim Args As String Dim ArgsList As Variant Dim ArgKey As String Dim ArgValue As String Args = StrRight(QueryString, "OpenAgent&") ArgsList = Evaluate ({@LowerCase(@Explode("} & Args & {"; "&"))}) ForAll Arg In ArgsList ArgKey = StrLeft(Arg, "=") ArgValue = StrRight(Arg, "=") AgentArgs(ArgKey) = ArgValue End ForAll DefaultExit: Exit Sub ErrorHandler: Call LogError() Resume DefaultExit End Sub
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at

* Of course the vCard text does not have to be dynamically created and could/should be stored in the company document for better performance.

Download agGetVCard.lss.