Pages

Monday, January 10, 2011

getWebDBName in XPages (@WebDBName in XPages)

The following is a function that helps u obtain the web db name of a database in xpages (@WebDBName)


function getWebDBName() {
var arg=@Trim(arguments[0]);
var webDBName="";
if (typeof arg!="undefined"){
webDBName=@ReplaceSubstring(arg,"\\","/");
return webDBName;
}
var dbPath=@DbName()[1];
webDBName=@ReplaceSubstring(dbPath,"\\","/");
return webDBName;
}


The following are the ways in which u can use the above function

getWebDBName("a\\b/c\\d.nsf");      //will return a/b/c/d.nsf
getWebDBName(database);             //will return @WebDBName;
getWebDBName();                          //will return @WebDBName;
getWebDBName(db:NotesDatabase);  // will return @WebDBName of the db object being passed

Hope this helps :)

6 comments:

  1. /**
    * provides functionality of the function with same name from @Formula
    *
    * @return the name of the current database in a websave format
    * @author Michael Gollmick
    * @version 1.2
    * @date 20090127
    */
    function @WebDbName() {
    try {
    if (typeof this.name === 'undefined') {
    var path = database.getFilePath();
    var re = new RegExp("\\\\", "g");
    path = path.replace(re, "/");
    var arr = path.split("/");
    for (var a = 0; a < arr.length; a++) {
    arr[a] = escape(arr[a]);
    }
    this.name = arr.join("/");
    }
    } catch (e) {
    }
    return this.name;
    }

    ReplyDelete
  2. I'll throw in mine as well :)

    function getWebPath( db:NotesDatabase ){
    try {
    db = db || database;
    return '/' + db.getFilePath().replace( /\\/g, '/' );
    } catch( e ){
    /*Debug.logException( e );*/
    }
    }

    ReplyDelete
  3. Thank you for sharing these functions. They are really great. :)

    ReplyDelete
  4. why not use: session.evaluate("@WebDbName")

    ReplyDelete
  5. @Thomas: session.evaluate("@WebDbName") works great,

    where as session.getHttpURL()for a database in my local hard drive returns,

    http://SystemName.MyDomainName.local?OpenServer.

    I would prefer an empty string or the name localhost to be returned here. I smell complications with getHttpURL() :)

    Thanks a lot for these suggestions.

    ReplyDelete