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;
    }
}
    
    
    

7/24/2012

Open the Users Mail Database

To open the mail database of the current user, just use the Method "OpenMail" in a NotesDatabase Object. It feels a bit awkward at first, but actually the object will update itself to contain the database immediately. Easy!

Dim db as New NotesDatabase("","")
Call db.OpenMail
Dim doc as NotesDocument
'now just prepare am email and open it in the client, ready to be sent.
Set doc=db.CreateDocument()
Call doc.ReplaceItemValue("Form","Memo")
Call doc.ReplaceItemValue("SendTo","John.Doe@Example.com")
Call doc.ReplaceItemValue("Subject","Hello World")
Call doc.ReplaceItemValue("Body","This Mail will now be opened and ready to be sent by the user.")
Dim ws as New NotesUIWorkspace()
Call ws.EditDocument(true, doc)

7/18/2012

How To Search your Workspace for Code

This is very useful for those code changes that just might span over different design elements. Just hit the "Search" Menu entry that is available in designer and enter the types of design elements that you want to search. Please notice that the standard setting will be quite slow, so here are some (source code) extensions you will want to include when developing xpages and notes. You will want to exclude everything that is binary, because you wont find anything in it.
  • *.xsp XPages and Custom Controls
  • *.js Client Side Java Script Library
  • *.jss Server Side Java Script Library
  • *.java Java Source Code
  • *.css CSS File
  • *.lss Lotus Script Library
  • *.lsa Lotus Script Agent
  • *.form Form DXL Representation
  • *.subform Subform DXL Representation
  • *.view View DXL Representation
All those other extensions can be found out in the "Package Explorer" view of the eclipse framework in the designer.