Getting a parameter from the URL of an XPage
Category xpages
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.
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: }
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
Posted by brunette hair color trends 2012 ideea At 12:51:47 PM On 10/26/2011 | - Website - |
Posted by Wedding program fans At 06:14:58 AM On 10/13/2011 | - Website - |
Posted by shoes spring 2012 At 06:56:22 AM On 10/01/2011 | - Website - |
context?getUrlParameter("query") will work with "url?Query=", "url?query=", "url?QUEry=" and so on. So I prefer using that method.
Posted by Philippe At 10:49:10 AM On 12/29/2010 | - Website - |
It might interest you to know that LotusUserGroup.org is hosting the XPages Help Documentation Team in a live forum this week. they're looking for people to give feedback just like this. You can post a comment, ask questions, and/or give them feedback on the effectiveness of the XPages help at { Link } (then click on the top "sticky" forum)
Posted by Scott Treggiari At 12:54:08 PM On 11/09/2010 | - Website - |
Posted by Patrick Picard At 10:36:46 AM On 10/29/2010 | - Website - |
Posted by Jacob At 05:13:33 AM On 06/07/2010 | - Website - |
The param object does not persist when navigating through views etc - at least it never used to.
Posted by Steve Castledine At 03:58:10 AM On 07/09/2009 | - Website - |
In that case, you can use another approach I documented here:
{ Link }
Posted by Julian Buss At 02:51:46 AM On 07/09/2009 | - Website - |
Posted by Jeremy Hodge At 09:44:20 PM On 07/08/2009 | - Website - |
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.
Posted by Tim Tripcony At 08:34:26 PM On 07/08/2009 | - Website - |