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.
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)
Call ExplodeQueryString (docContext.Query_String_Decoded(0), AgentArgs)
If IsElement( AgentArgs ("id") ) Then
strCompanyID = AgentArgs ("id")
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
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)
Print |content-type: text/x-vCard; charset=iso-8859-1|
Print |content-disposition: attachment; filename="| & varEvaluateResult(0) & |.vcf"|
Print
Print strResult
Exit Sub
ErrorHandler:
strResult = ||
Call LogError()
Resume DefaultExit
End Sub
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 nsftools.com. * 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.