Wednesday, February 27, 2013

How to launch an attachment from a view control

I need to show a visual indicator whether a document in a view control had an attachment.   I wanted the visual indicator to act as a link open the attachment.   I could not find anyone on the net who did this just the way that I needed it, so I had to make it myself.

Here is how it works:

The document view control looks like this to the user.  I download the paper clip from a free clip art site.
When the user clicks the link, it launches the attachment.  Amazingly it actually works better in Internet Explorer.  It launches a blank window in the background, and the the File Download dialog box with the option to Open Save or Cancel.  I made the blank window to be height=120,width=650.



In Chrome, the same window is launched, but looks like this.   The user has to choose to download the file, then launch.   If I didn't make the window 650 pixels wide then the user would have to expand the window to see the Keep/Discard question.  The user would then have to double click on the file to open it.


How did I code it:

There are two main places where I had to add code.

1) Before I added anything, I needed to give a variable name in the view control.   This gives me a handle to each particular row.



2) The column data is bound to a null value ("").  This necessary to prevent errors.

3) Add the following function to your SSJS script library.  Credit for this belongs to Stephan Wissel.  I found this on his blog.  I modified it a bit to account for storing my data in a separate database.

function getAttachmentURL(docID:java.lang.String, attachmentName:java.lang.String, databaseName:java.lang.String) {
    var base = databaseName
    var middle = "/xsp/.ibmmodres/domino/OpenAttachment/";
    if (base.substr(0,4) == "/xsp") {
        middle += base.substr(4);
    } else {
        middle += base;
    }
    var result = base + middle + "/" + docID + "/$File/" + attachmentName + "?Open";
    return result;
}


4)  In the View Column properties under Display, I added code to the Column Image.  In my case the filename is always the same.   This only shows the image if the attachment is present.   In this project, the attachment is required, but I wanted to make it reusable for cases when it isn't required.


var doc:NotesDocument = rowValue.getDocument()
var attachment = doc.getAttachment("storetransfer.pdf")

if (attachment === null ){
return ""
}
else{
return "/attachment.gif"
}


5)  In the onClick event of the column, you have the following code tied to a Full Update.  If you don't know the postScript method, then definitely read up on it.   It allows you to run clientside javascript after serverside javascript processes.  It is a very handy tool in your toolbox.


var doc:NotesDocument = rowValue.getDocument()
var attachment = doc.getAttachment("storetransfer.pdf")

if (attachment === null ){
//print("no attachment")
//do nothing here
//theoretically this should never happen since attachment is required
//in addition, the paperclip icon is hidden.
}
else{
var unid = rowValue.getUniversalID()
var url = getAttachmentURL(unid, "storetransfer.pdf", "database.nsf")
url = "/" + url + ";"
view.postScript("window.open('" + url + "', '_blank', 'height=120,width=650,top=10,left=10,resizable=yes');");
}

No comments:

Post a Comment