Getting the Base URL of the Current Database with SSJS
It’s easy to get the current page URL in SSJS with
context.getUrl().toString(). However, it’s a little less straightforward if you just want to determine the base URL of the current application. This can be useful when you need to build a link to some other page within the current application and send it out in an e-mail notification. In this post, I’ll look at the results of some methods in the
XSPUrl object and show how to achieve the desired effect.
In the last post, I looked at URL @functions in the Extension Library. There are some useful functions there, but none that retrieve what I’m looking for.
context object has a
getUrl() method that makes it easy to get a handle to an
XSPUrl object representing the current URL. You can retrieve the full URL (including the querystring) or use any of the methods available to get pieces of the URL (
getAddress(), getHost(),getParameter(), getPath(), getPort(), getQueryString()).
Here are some examples of the output, given this url: http://127.0.0.2/BlogTesting.nsf/URL.xsp?parameter1=a
There are several handy methods, but none providing exactly what I’m looking for.
Options for Getting the Base URL
getAddress() method gets the URL up to, but not including, the querystring. However, it still includes the current page name. The global
view object has a
getPageName() method that returns the name of the currently-displayed XPage.
The snippet that I’ve been using recently uses the
getAddress() method and replaces the current page name with an empty string, leaving us with the base URL:
substring methods to look for the ‘.nsf’ and only retrieve characters up to the end of it in most cases, but that may be problematic if you have routing that masks the NSF name.
What’s Your Solution?
This seems like something that probably has a number of workarounds. How have you solved it? Is there something simpler that I’ve overlooked?
Check out David Leedy’s XPages URL Cheatsheet for more URL-related functions http://xpagescheatsheet.com/cheatsheet.nsf/url.xsp
There are two listed on there that sounded like they might provide what I was looking for, but did not.
- database.getHttpURL() – includes the database replica ID rather than name and appends ?OpenDatabase to the end
- view.getBaseURL() – sounds perfect, but doesn’t return anything