Categories





IM me on the bleedyellow.com sametime community - jeremy.hodge@zetaone.com

« Ok, here is your first look @ YellowBubble.org | Main| xPages Bug with XSPUrl.GetParameter() ?? »

Getting a parameter from the URL of an XPage

Category
0
Sometimes, I think the biggest hurdle in learning xPages is the documentation. Its not the easiest thing to find (it doesn't appear to be in the the 8.5 designer help, but only in eclipse style help, or at least I can't find it in NSF form.)  Anyway, I figured as I searched through and found some answers to common questions, I'd post them here to hopefully help guide others and prevent them from having to search as long as I had to just to find an answer.

This post is about the server side javascript required to get the value of a parameter sent in the url.

To do this, we are going to use the XSPUrl object.  Here's the code:

 1:
 2: var url:XSPUrl;
 3:
 4: try {
 5:        url = context.getUrl();
 6:        var n = url.getParametersCount();
 7:        if (n > 0) {
 8:                var tagname = url.getParameter('tag');
 9:                return "View By Category - " + tagname;
10:        }
11: } catch(e) {
12:        return "View By Category";
13: }


The use case concept here is we are going to display a set of documents from a view based on a selected category, passed on the URL in the form of "http://www.yourdomain.com/db.nsf/Your.xsp?tag=TagValue". The above code returns a string that, for example, could be used as a page heading, including the category passed in, if available.

Line 2 strong types the variable url as the type XSPUrl which has several functions to manipulate URLs (Imagine that!).
Line 4 is try...catch block to do some error checking. If anything goes wrong, we'll just return a generic string,
Line 6 assigns the url object from the context object's URL
Line 7 is really just for an expanded example. It shows you how you can get the number of parameters included in the URL. In our example, this would return 1.
Line 8 retrieves the value of the 'tag' parameter and stores in a variable 'tagname'
Line 9 returns the string
If at any point an exception is raised in lines 5 through 10, line 12 executes and returns the generic title.

Happy Coding!

Comments

Gravatar Image5 - I agree that Xpages may cause a lot of problems in the beginning, when you are just learning to operate with Lotus. But step by step everything settles on its own

Gravatar Image4 - In my experience context.getUrlParameter("param") is the best method here.

The param object does not persist when navigating through views etc - at least it never used to.

Gravatar Image3 - there are some cases and events where .getUrlParameter() does not work.

In that case, you can use another approach I documented here:

{ Link }

Gravatar Image2 - Nice ... I can't believe i missed it right there in the globals ... Much easier.

Gravatar Image1 - As a shortcut, there's also a global variable named param that supports the "extended class" syntax. So, for example, param.tag would return the same value as context.getUrl().getUrlParameter("tag").

If you want to code defensively to account for the possibility that the parameter is undefined, you could use the following:

var tagname = (param.tag || "");

This will return the value of the parameter if one was provided, and fail over to the empty string if the parameter was omitted.

Post A Comment