Hello,

just a quick tipp - I was looking for a solution on how to launch attachments in an XPINC application without downloading them first and eventually came up with a solution. My problem was, that I had multiple attachments in the documents I had to deal with. The attachments were Office documents, PDFs and even MP4 video files, so a wide range of formats.

The basic idea behind my solution is to use a notes:\\\ Link to open the corresponding Notes Document from my XPINC app and let LotusScript handle the process of launching the attachment. My only problem was how to let LotusScript know, which attachment I wanted to launch. One idea was to create temporary documents and to use the launch first attachment functionality, but I did not want to create and delete additional documents.

So, here's what I came up with in a nutshell: Environment variables. Still remember them ? Yeah, back in Notes 2 and 3 they were huge, but they are still around and guess what - even XPages can work with them. So, to handle my attachments, I created a repeat control for a table, that lists all the attachments in the Notes backend document as one table row. This look like this:

Image:Launch Attachments in XPINC app (Windows only !)

This is the SSJS to select all my attachments for the repeat control:

var alist = document1.getAttachmentList("Body");

if (alist.isEmpty()) {

     return null;

} else {

     
     var alisti = alist.iterator();

     var names = new java.util.Vector();

     while (alisti.hasNext()) {

             var content:String = alisti.next().getName().toString();

             names.add(content);

     }

     return names;

}


The control renders a link for each attachment showing its name. The link contains an onClick-Event that takes care about creating the notes:/// link to the document itself and to set an environment variable with the name of the attachment to launch. I use the view.postscript construct to redirect to the notes document, this automagically opens in a new notes window.

Here's the code for that, "docs" is my variable from the repeat control that carries the name of the attachment, document1 is the document datasource:

var aname:String = docs.toString();

var replid = database.getReplicaID();

var baseurl:String = "
notes:///"+replid+"/0/"+document1.getDocument().getUniversalID()+"/";

var url:String = baseurl+"?OpenDocument&AttachmentName="+docs.toString();

session.setEnvironmentVar("CurrentAttachment",""+aname);

view.postScript('window.location.href="'+url+'"');


In Notes, I have used the ShellExecute command - I remebered the shellexec.lss of Julian Robicheaux, you can find it here: http://www.nsftools.com/tips/WinTips.htm

So, in the Notes Document, I used the QueryOpen Event to launch the attachment and stop the Notes Document from opening - et voilà !

Image:Launch Attachments in XPINC app (Windows only !)

Here's my code in the query open event:

Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)

     On Error Goto fehler

     Dim doc As NotesDocument

     Dim obj As NotesEmbeddedObject

     Dim aname As String

     Dim AgentArgs List As String

     Dim db As NotesDatabase

     Dim s As New NotesSession

     Dim result As Long

     Dim rtitem As Variant

     
     Set db = s.CurrentDatabase

     Set doc = Source.Document

     
     
     aname = s.GetEnvironmentString("CurrentAttachment")

     'Msgbox "File: "+aname

     
     Set rtitem = doc.Getfirstitem("Body")

     If (rtitem.Type = RICHTEXT) Then

             Set obj= rtitem.GetEmbeddedObject(aname)

             aname = "c:\windows\temp\" & aname

             Call obj.Extractfile(aname)

             result = ShellExecute(0, "open", aname, "", "",1)        

     End If

     
     
     Goto raus

fehler:

     Msgbox Error$+" at line: "+Cstr(Erl())

     Resume raus

     
     
raus:

     continue = False

End Sub


As always - there are tons of ways to do this better/easier/with less code - let me know. I have been looking around for some time now and this is the best I came up with so far.

Happy coding !

Heiko.
Heiko Voigt   |   3 March 2014 12:54:59   |    Notes  XPAGES  XPINC    |  
  |   Next Document   |   Previous Document

Comments (0)