« Learning XPages Part 6 : Showing The Username In The Banner | Main| Google Desktop Search Causes Notes SmartUpgrade Failure. »

Learning XPages Part 7 : The Banner Login/Logout Button

Tags : Lotus Domino XPages JavaScript
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

In the last part of the series we saw how we could just some simple server side javascript with code that looked very much like a mix of LotusScript and formula language to show the name of the currently logged in person above our application in the banner. In this part we will finish off the banner by providing the user and way to login or logout of the application.

Lets start by opening our layout_Banner custom control again. You should see the list that we created in the last part, remove the second bit of placeholder text and drag in a new control form the controls pane on the right side of the screen. the control we are going to use this time is a 'Link' control. Drag it in and position it where the second bit of placeholder text in the list used to be.

A picture named M2

Once you have it in place ( which you can confirm by using the source view of the custom control to make sure it appears inside the <li> & </li> tags like in the above image have a look at it's properties. There are quite a few different properties that we can set but we are only interesting in two of them, The label to make it say 'Login' or 'Logout' and the URL that the end user will be directed to when they click the link.

Almost every property in an XPage control can be computed. Just look for the little diamond at the end of each property. If the diamond is a solid blue color then you know that the property is using a computed value.

A picture named M3

We are going to compute the value for the link so click the diamond and select the 'compute value' option. You'll see the JavaScript entry dialog box appear on your screen and we are going to use the following code :

var userName:NotesName = session.createName(@UserName());
if (userName.getCommon()=="Anonymous")
{
       var rtnvalue = "Log In"
}
else
{
       var rtnvalue = "Logout"
}
return(rtnvalue);


Here we set the userName variable in the exact same way as we did for the other part of the banner but we then use a standard IF THEN ELSE style statement to return either login or logout as a string. For the URL we can do pretty much the exact same, click the diamond to set it to a computed value and use the following code :

var userName:NotesName = session.createName(@UserName());
if (userName.getCommon()=="Anonymous")
{
       facesContext.getExternalContext().getRequestContextPath() + "?login"
}
else
{
       facesContext.getExternalContext().getRequestContextPath() + "?logout"
}


This time instead of returning a simple string we are going to construct the returned value. The .getRequestContextPath on the externalContext is a way to figure out what URL the user typed in to get to your application. The '?login' and '?logout' that is appended to the end are just standard Lotus Domino URL constructs to log a person in or out of a database. You can use these constructs today on non-XPage applications also. By using the external context path we don;t need to hardcode anything into the application so you can move the database to any server, path or filename.

Lets save the the XPage and preview it in our web browser to see how it looks :

A picture named M4 or A picture named M5

We have now completed the first layout section of our XPage application. Included below is a copy of my development database that contains all the design changes that we have made so far in the series. We have covered quite a few topics so far including how to break your application up into custom controls to make it easier to include different parts of the layout in pages, we have looked at how to use an image control and a how to mix html with xpages to produce an unordered list and within that list we used a computed field control and a link control in combination with some server side javascript to control what we wanted displayed.

In the next part of the series we'll start working on the next part of the layout. the 'Title Bar'.

Download File xPhoneP7.zip

Comments

Gravatar Image1 - Hej,

I just wanne say that I think these serie of postings are one of the nicest and most contributing learning XPages and everything around it (from a XPage beginner perspective).

So thank you and keep up this good work!

Gravatar Image2 - Declan, let me add to the chorus of thank yous, this is wonderful stuff (and it really sells xpages).

BTW, at this point you really need to be working on the server, since running it locally won't allow you to handle authentication.

Gravatar Image3 - Declan, thanks for the good work you are doing with this series.

One small remark about this lesson:
when you compute the URL for the login/logout link, the ?login and ?logout URL commands will work if the Domino http is set to session authentication; with basic authentication you have to use ?open&login .


Gravatar Image4 - Excellent! I haven't done any type of web development for Notes in years and pretty much forgot all my "stuff". Not only am getting some good XPages learning here, but I'm refreshing my HTML and Javascript. Very cool that they have aligned the @Functions with the Javascript coding.
Emoticon

Gravatar Image5 - Declan, thansk for the effort here I am learning a lot. I put the computed field in like you show for the login & logout and when I click login it opens up my default login screen then when I login it opens my email in a browser. I am sure I have a preferences or something set wrong but just not sure what. I also downloaded your version and tried it as well with the same result. Can you point me in the direction of my mistake?

thanks, Steven

Gravatar Image6 - Thanks for all the effort to share this with us struggling developers Emoticon

One question: My application has regular Lotus Notes forms, webforms and XPages.
Now I've added the login function to my XPage startpage as facesContext.getExternalContext().getRequestContextPath() + "?open&login", but when I login, it will redirect me to the regular webforms startpage instead of the XPages startpage. Any ideas? I don't want to remove the webforms yet since we don't have the XPages version fully working.