Skip to main content link. Accesskey S

The useful resource for IBM Lotus Domino XPages development

Submit Search


Home > Formula > Useful formulas and code snippets
xpageswiki.com
is maintained by 
Julian Buss.
You can hire me.

Useful formulas and code snippets

ShowTable of Contents
Here are some various snippets.

Get the current username


Use
session.getEffectiveUserName()
to the the name of the user who is logged on.
Use @Name() to convert it for exampel to a CN name:

@Name("[CN]", session.getEffectiveUserName())

Check if a user contains a certain role


context.getUser().getRoles().contains('[RoleName]');

Redirect to another XPage


context.redirectToPage("Page.xsp")


Reload a XPage every x seconds


in "beforePageLoad" event of the XPage:

var r = facesContext.getExternalContext().getResponse();
r.setHeader("Refresh", "1");


OR as meta tag: in "all properties" of the XPage -> resources -> add "metaData" with:

- content = 1
- httpEquiv = Refresh
- name = Refresh
- rendered = true


TimeDate handling

Set current datetime to a field in a document


var dt:NotesDateTime = session.createDateTime("Today");
dt.setNow();
doc.replaceItemValue("created", dt);

Get only the time of a datetime value


var nw = @Now();
return nw.toTimeString()

String handling

Replace all occurrences of one string by another


Use string.replace(searchstring, replacestring), this replaces all occurrences.
There seems to be a problem with the backslash \, this does not work:

s = s.replace("\\", "/")


The \ is only replaced once.
Workaround: use @ReplaceSubString(s, "\\", "/").

Formatting numbers


formatting with grouping and fixed fraction number


You want a number of for example 1000000,4 to format with thousand-grouping and two fraction numbers for the de language:

	locale = new Locale("de");
	return I18n.toString(v, "###,###.00", locale );


Caution when converting a string to a number


If you try to convert a value from getComponent(..).getValue() to number with the Number() function the XPage crashes.
The reason is that getComponent(..).getValue() returns a java.lang.string, but Number() does only work with JavaScript strings.

Use this workaround:

var v = getComponent("field").getValue();
v = new String(v);
i = Number(v);


Code about URLs


moved to Work with URLs

Changing DOCTYPE


Since 8.5.1 you can change the doctype of the rendered HTML.
See Declans Blog for details.

- Open the Eclipse package explorer (Windows -> open view)
- Edit to you database -> WebContent -> WEB-INF -> xsp.properties
- Add the following line:

xsp.html.doctype=<!DOCTYPE ...>


Set the DOCTYPE as you like.

Removing Dojo and the standard FORM tag


From Chris Toohey

Follow these steps to create a XPage without any reference to Dojo and without the standard form tag:


  1. Create a new theme and remove the line "extends=webstandard".


  2. Set this theme in the Application Properties.


  3. Open the Package Explorer view (window -> open view), browse to your database, edit Web Content/WEB-INF/xsp.properties and add the line "xsp.client.script.libraries=none".


  4. Create a new XPage and set All Properties/Basics/createForm to false. In the source view manually add createForm="false" to the xp:view tag.


Create sequential numbers


See this link

Cross Domain AJAX / XMLHttpRequest and Domino


Lotus Domino has it's own proxy server to solve cross domain issues with ajax requests. See Declans Blog for details.

Sort an array of strings case-insensitive


myarray.sort(function (a, b) {
if (a.toLowerCase() > b.toLowerCase()) return 1;
else if (a.toLowerCase() < b.toLowerCase()) return -1;
else return 0;
}
);

Add meaningful page titles to Google Analytics URLs


If you use Google Analytics, you can use this code to give Google meaningful page titles instead of Domino URLs (right after the _gaq.push(['_setAccount', '...']) call):

_gaq.push(['_trackPageview', 'my meaningful page title']);

@BusinessDay() in SSJS


var startDate=getComponent("StartDate").getValue()
var endDate=getComponent("EndDate").getValue()
var daysToExclude=getComponent("Wday").getValue()
var datesToExclude=getComponent("HDay").getValue() 
var sDate:NotesDateTime=session.createDateTime(startDate) 
var eDate:NotesDateTime=session.createDateTime(endDate) 
var pubarray=new Array();
var daystoexclude = @Explode(daysToExclude,";"); 
daystoexclude=@Implode(daystoexclude,":");
var hday=@Explode(datesToExclude,",")
var hdayarray=new Array();
if(@Count(hday)==0){
hdayarray=""
}
else{
for(i=0;i<@Count(hday);i++)
{
var temp:NotesDateTime=session.createDateTime(hday[i])
hdayarray[i]="["+temp.getDateOnly()+"]"
}
}
var pubdays=@Implode(hdayarray,":") 
var sDatesquare="["+sDate.getDateOnly()+"]"
var eDatesquare="["+eDate.getDateOnly()+"]"
var formula=""
if(pubdays=="")
{
formula="@BusinessDays( "+sDatesquare+";"+eDatesquare+";"+daystoexclude+")"
}
else
{
formula="@BusinessDays( "+sDatesquare+";"+eDatesquare+";"+daystoexclude+";"+pubdays+")"
}
session.evaluate(formula);
 

give 1:7 for the weenend failed, to exclude saturday and sunday.

Create By Md Arif Iqbal@TcsBangalore

Per Henrik's XPages boilerplate


Per Henrik Lausten published a nice list of useful XPages snippets: useful code snippets and modules for your next XPages project
Created by Anonymous on Jul 8, 2009 10:12:06 AM

change

GetUrlParameter

into

getUrlParameter

or it will not work....


Created by Julian Buss on Jul 28, 2009 11:25:22 AM

done, thanks!


Created by Anonymous on Jul 30, 2009 8:30:15 PM

So what I find confusing about this page is what computer language these examples are written in. For example in the "converting a string to a number" it says that "getComponent(..).getValue() returns a java.lang.string" but this looks like JavaScript, not Java.

Suggestion: At the top or on each snippet, as approprate, place some information to qualify the code.

A. Where is this code executed: client or server?

B. What language are they in: Java, JavaScript, XPage markup, @ formula?

Another question: some of these snippets have a "return" but do not appear to be inside a subroutine. Is the "return" necessary or would it be better to leave it off of the example?

Thanks for a helpful web site.

Rob:-]


Created by Julian Buss on Aug 11, 2009 10:20:58 AM

regarding language: normally it's JavaScript. Sometimes combined with @formulas. And somethings even combined with Java. :-)

In Xpages, you cannot say "this is JavaScript" or "this is Java" because it's all mixed:

- You use @formulas in server side javascript since @formulas are only shortcuts to equivalent javascript methods

- You can use backend java code from server side javascript since all the server side javascript is translated into java in the backend

That may be confusing at first. But if you know that, it's very cool and offers endless possibilities :-)


Created by Anonymous on Sep 21, 2009 8:40:05 AM

RE: Get an URL parameter

What about when there is something like & or another HTML entity in the query string?


Created by Julian Buss on Oct 12, 2009 9:48:07 AM

don't know. not tested :-)


Created by Anonymous on Sep 9, 2010 5:18:43 PM

I don't think that the Reload a XPage every x seconds works in the client, works well on the web, thanks, Sean Cull


Created by Eric T on Sep 24, 2012 3:29:09 PM

The link for sequential number via ssjs is pointing to an incomplete blog article with the actual code cut off. I could not leave a comment nor was there a link to contact the author.

Link: http://www.qtzar.com/blog/in00zx5i0r9c/


Created by Randal Oulton on Nov 29, 2012 2:20:22 AM

The link provided for sequential numbering isn't useful, as that blog post is truncated, lol. (And yes, Eric T. no way to contact aside from twitter.)

The reposted code by the author Declan Lynch is here, and it works: http://fatalweb.com/question/adding-numbering-to-documents-in-xpages-9805356.html


Created by Sean Haggerty on Feb 6, 2013 10:16:07 PM

Another way to convert string or String to number is to multiply the variable by 1

someVariable*1 =numeric_someVariable


Created by Broken link to Declans Blog on Oct 29, 2013 12:56:19 PM

Link to Declans Blog is not working.


Add Comment

Name:
Comments:
How to take your XPages App to the iPhone, iPad, Android: use Domino To Go!
Do you look for an XPages Workflow solution? Take YouAtNotes Workflow.