This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.

Dec 4, 2011, 1:39 AM
64 Posts

Get User Session in Xpages using JavaAgent/JavaBean

  • Category: Managed Beans in NSF
  • Platform: Windows
  • Release: 8.5.2
  • Role: Developer
  • Tags: Xpages,JavaAgent,JavaBean
  • Replies: 0

Get Sessioned-User

Xpages/JavaAgent/JavaBean/Domino Designer

System Requirements:

Download Domino Designer 8.5.2 Environment (DDE)


Build a complete application that retrieves a sessioned-user with a JavaAgent, creates an XML file to be read by a JavaBean, feeds into an Xpages form.

Step 1:

Use JavaAgent, retrieve user in session:

Copy and Paste below JavaAgent over the skeleton version generated by DDE. You will not need to do much in properties to run this one, no docs need returned; add secure user to run on behalf of.


* Created: 2011.

* JavaAgentdataAsXML

* XML data for Xpage to be read by JavaBean


//load imports

import lotus.domino.*;



* @author kcherizard

* @version 2011.



// begin class

public class JavaAgent extends AgentBase {

public void NotesMain() {

// let's add a try catch here...

try {

// open our session...

Session session = getSession();

// load info console for debugging purposes

System.out.println("We've got a session..." + session);

// load agentContext

AgentContext agentContext = session.getAgentContext();

// load info console for debugging purposes

System.out.println("We've got agentContext..." + agentContext);

// Declare/Initialize logged on user

String SystemsUser = session.getCommonUserName();

System.out.println("We've got a UserName..." + SystemsUser);

// Create a Java PrintWriter object variable

PrintWriter pw = getAgentOutput();

// Set the content type



// Write out the HTML Opening Tags

pw.println("<?xml version='1.0' ?>");







// close the root tag


} catch (Exception e) {





Step 2:

Write a JavaBean, read JavaAgent session value:

Copy and Paste code and go through to ensure you understand what the code is doing, good practice... You are going to read an XML file and will pass a session value to the variable personName.

package com.osc.blackberry.test;


* Created: 2011.

* Test JavaBean to read XML file into Xpages


* Modifed: 2011. to reflect emailaddress as opposed to fullname,

* item to be used by Ticket System



* @author kcherizard

* @version 2011.



import javax.faces.context.FacesContext;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class XMLUserBean {

static String UserID;

static String PersonName;


* @return the personName


public String getPersonName() {

//entering try catch block

System.out.println("entering try catch block...");

try {

//initialize builder and grab XML file

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

//load data through via URL

Document doc = db



System.out.println("Root element "

+ doc.getDocumentElement().getNodeName());

//retrieve person tag

NodeList nodeLst = doc.getElementsByTagName("person");

for (int s = 0; s < nodeLst.getLength(); s++) {

Node fstNode = nodeLst.item(s);

if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

Element fstElmnt = (Element) fstNode;

//search emailaddress tag

NodeList fstNmElmntLst = fstElmnt


//@Author: Köll S. Cherizard

//@version: 2011.

//changed from fullname to emailaddress

Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

NodeList fstNm = fstNmElmnt.getChildNodes();

System.out.println("Sessioned User : "

+ ((Node) fstNm.item(0)).getNodeValue());

UserID = ((Node) fstNm.item(0)).getNodeValue().toString();

//Ensure the session value is available

//TO DO: Uncomment this item

//(if (PersonName==null){)

//when you are comparing session against

//data from names.nsf file using same JavaAgent.

//@version 2011.

/*if (PersonName==null){

// call FacesContext to redirect to page in question




//load the session value from JavaAgent to personName variable

PersonName = UserID.toString();



} catch (Exception e) {



return PersonName;



* @param personName the personName to set


public void setPersonName(String personName) {

PersonName = personName;


public void retrieveXMLData() {

//initialize search for user session value from file




* @return the userID


public static String getUserID() {

return UserID;



* @param userID the userID to set


public static void setUserID(String userID) {

UserID = userID;



Step 3:

Building an Xpages file:

Let's load the user session value to Xpages, we will need to connect the Xpages form to above JavaBean using a faces XML config file. For now, just Copy and Paste below code for the Xpage; using 'beforePageLoad' ensures the value will load to Xpages before the page loads. xpnotauthorized.xsp is rendered when session is null, where names.nsf data did not compare against the session picked up by the initial user search.

    <?xml version="1.0" encoding="UTF-8"?>

<xp:view xmlns:xp="">


<xp:dataTable rows="30" id="dataTable1" var="XMLUserBean"



<xp:pager partialRefresh="true" layout="Previous Group Next"

xp:key="header" id="pager1" for="dataTable1">


<xp:pager partialRefresh="true" layout="Previous Group Next"

xp:key="footer" id="pager2" for="dataTable1">



<xp:column id="column2">


<xp:label value="Person Name" id="personName1" xp:key="header">



<xp:text escape="true" id="computedField2"

value="#{javascript: XMLUserBean.personName}"></xp:text>





Step 4:

Configure faces-config.xml file:

Faces config file bridges the JavaBean and the Xpages form. Copy and Paste XML code into your environment's faces file.








Run it:-)


We recommend logging on to names.nsf, grab values and compare against the sessioned-user, and forward the user's id, or which ever column you specified to file, to ensure the user is a valid networked-user. This item can also be added to a session (facesContext) within the JavaBean, to be used throughout the application.


Happy Coding!

This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.