11/09/2012

Open Attachments In A XPage In Client Application

I recently stumbled about how to open a file attachment like a picture, if you are in a xpage in client application - the url is a completely different one than that in a browser, and it is pretty much undocumented.
In my case, i even had to access an attachment in a differten database, what adds another layer of complexity.
So in addition to the infos in the post "Attachment URLs in XPages", i discovered the complete, correct format how to do it.

http(s)://127.0.0.1:[theRandomXPINCPort]/xsp/[currentServer]!![yourCurrentApplication.nsf] /xsp/.ibmmodres/domino/OpenAttachment/[yourTargetServer]!![yourTargetapplication.nsf]/[UNID]/$File/[AttachmentName]?Open

First part is easy - your local host and the image of the xpage on the server and database you are  just working in. Then add the openattachment url part, then the target server and database.

Easy - when you know it. Have fun!

As a bonus, here is a complete library code, that does the magic for you, modifying the code from the post "XPages in Notes Client!! How to build URLs".


var debug = false; 
/**
Builds path to an attachment
*/

function getAttachmentPath(doc:NotesDocument, attachmentName, rtFieldName){
 if(rtFieldName==null || rtFieldName ==""){
  rtFieldName = "$File";
 }
 if(context.isRunningContext("Notes")){
     var dbPath = getDbPath();
  var dbPathTarget = getDbXPInCPath(doc.getParentDatabase());
     return "" + dbPath[0] + "/xsp/.ibmmodres/domino/OpenAttachment/" + dbPathTarget + "/" + doc.getUniversalID() + "/"+rtFieldName+"/" + escape(attachmentName)+ "?OpenElement";
 }
    else{
     var url = doc.getHttpURL();
  return url.replace("?OpenDocument","/$File/"+attachmentName+"?OpenElement");
    }
}

/**
Cache the XpagesInClient path variables in an applicationScope variable
*/
function getDbXPInCPath(db:NotesDatabase){
    var dbCacheName = "dbpathxpinc-"+@LeftBack(db.getFileName(),".");
    if(debug||isCacheInvalid(dbCacheName, 600)){
     synchronized(applicationScope){
      var res = db.getServer()+"!!"+db.getFilePath().replace("\\","/");
      applicationScope.put(dbCacheName,res);
     }
    }
    return applicationScope.get(dbCacheName);
}

/**
Cache the dbPath variables in an applicationScope variable
*/
function getDbPath(){
    if(debug||isCacheInvalid("dbpathweb", 600)){
     synchronized(applicationScope){
      var dbPath = @Left(context.getUrl().toString().replace("\\","/"), ".nsf") + ".nsf";
      var pos = (context.isRunningContext("Notes")) ? 4 : 3;
      var secondPathElements = dbPath.split("/");
      var secondPath = "";
      for (pos; pos<secondPathElements.length; pos++){
       if (secondPath != "")
       secondPath += "/";
       secondPath += secondPathElements[pos];
      }
      var res:Array = new Array();
      res.push(dbPath);
      res.push(secondPath);
      applicationScope.dbpathweb = res;
     }
    }
    return applicationScope.dbpathweb;
}

/**
A generic caching mechanism for each key will check to see if it is 'n' seconds
since it was last updated. Use for things that change relatively infrequently  
*/
function isCacheInvalid(key, cacheInterval){
    var currentTime = new Date().getTime();
    if (!applicationScope.containsKey(key + "_time")){
     applicationScope.put(key + "_time", currentTime);
     return true;
    }
    var diffInSecs = Math.ceil((currentTime - applicationScope.get(key + "_time")) / 1000);
    if (diffInSecs < cacheInterval) {
     return false;
    } else {
     applicationScope.put(key + "_time", currentTime);
     return true;
    }
}