Today I picked up a query on the Notes and Domino 8.5 forum asking how to launch the first attachment on a document from a view panel in XPages. I had done something similar for BLUG in terms of adding content to a view panel and showing a thumbnail picture stored in a rich text field. I’ve done something similar in classic web development, using @AttachmentNames in a view and a quick look at the help confirmed that @AttachmentNames is one of the @Formulas available in XPages. I instantly offered a suggestion that included the following server-side javascript code to identify the url to open the attachment, where myVar is the variable name assigned to the view panel:

“./0/” + myVar.getDocumen().getUniversalID() + “/$FILE/” + @Subset(@AttachmentNames(),1)

However, after further investigation the help confirmed that @AttachmentNames can only be used to get the attachment names from the current document and not, as I was suggesting, a particular entry in a view. The simplest option is to do something along these lines to generate the url in the view itself, so:

“./0/” + @Text(@DocumentUniqueID) + “/$FILE/” + @Subset(@AttachmentNames;1)

If the column is given a programmatic name such as “attachLink”, the following can be done to open the first attachment on the web:

<xp:viewPanel var=”myVar” rows=”30″>
<xp:viewColumn id=”viewColumn1″ value=”” columnName=”attachLink”>
<xp:link id=”link1″ text=”Launch Attachment” target=”_blank”>
<xp:this.value><![CDATA[#{javascript:myVar.getColumnValue(“attachLink”)}]]></xp:this.value>
</xp:link></xp:viewColumn></xp:viewPanel>

If you want to use a paperclip icon as the image for the link, you can easily do this by picking up one of the server-based images, as I showed with this tip.

As ever with XPages there are always alternative ways of doing the same thing, and my first thought was slightly more complex, but worth offering as it shows another interesting technique – a repeat control within a view panel. It also could be of benefit to get the first attachment in a field, when that attachment may not be the first attachment in the document. The concept was to use server-side javascript to get the attachment name, rather than modifying the view. I couldn’t do it directly, but I was able to do it by inserting a repeat control within the column to pick the first attachment name from a rich text item. Again it requires “myVar” to be set on the view panel. Again, the column needs to be bound to an actual column in the view (I’ve used the same column name, but you’ll need to point it to a column in your view) and value set to a blank string. But here is the column code:

<xp:viewColumn id=”viewColumn1″ value=”” columnName=”attachLink”>

<xp:repeat var=”attachment” rows=”1″>

<xp:this.value><![CDATA[#{javascript:try{var rtItem:NotesRichTextItem = myVar.getDocument().getFirstItem(“Attach”);
return rtItem.getEmbeddedObjects()} catch(e){}}]]></xp:this.value>

<xp:link id=”link1″ text=”Launch Attachment” taregt=”_blank”>

<xp:this.value><![CDATA[#javascript:”./0/” + myVar.getDocument().getUniversalID() + “/$FILE/” + attachment.getName()}”]]></xp:this.value>

</xp:link></xp:repeat></xp:viewColumn>

So within the column I am creating a repeat control with a single row. The data for the repeat control is an array of embedded objects from the Attach field on the relevant document. This is because the getEmbeddedObjects method of the NotesDocument class does not give attachments, so we need to get the actual field. If you have a number of rich text fields, you can use a loop to return the first attachment processing the fields in the order you wish, quitting as soon as you have at least one attachment. If a document does not have attachments or does not have an Attach field, the repeat will be blank and no link created. Within the repeat I’m then inserting a link to build the url using the current NotesXSPViewEntry’s UNID and the name of the attachment.

Two points to note about this. Firstly, you cannot use this to get an attachment that is not stored in a rich text field. And secondly, as mentioned it will not work for XPages in Notes Client. This is because the url outputted will be something like this:

http://127.0.0.1:1808/xsp/localhost!!Test.nsf/0/F919EDB3858C5995802576F0005C1B66/$FILE/doc.pdf

I believe from speaking to other XPages developers that it is possible to use an XPages function to generate the relevant link, but need to investigate further.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.