Return to the Last View Opened in XPages

There are many ways of handling navigation between pages in an XPages application. This post describes a technique that I’ve used to provide a way for users to return to the last (page containing a) view after closing a document. This isn’t specific to any type of data display; the concept is the same regardless of whether you use a data view, repeat control, view panel, data grid, etc.


There are a few things to consider when deciding how to handle the page flow:

  • You often don’t want to hard-code the return destination (e.g. always force users to return to the home page of an application). Especially if coming from the Notes client, the user is accustomed to returning to the view in which they clicked a link to open a document.
  • If you’re not using a ‘back’ button, you need to know where to return.
  • ‘Back’ buttons can be problematic because, when you go back, the page isn’t refreshed the same is if it were reloaded.
  • If the user adds or modifies a document, they may wonder if the update was successful if they don’t see the update when they return to the view.
  • This isn’t a concern if your application happens to open documents in separate browser tabs, because they’ve never left the view page. However, the view will not be updated with a new or modified document if it isn’t refreshed.

The Concept

Here are the steps required for a simple solution, at a high level:

  1. When opening a view, log the page name in a scope variable.
  2. From your form button, call a function to return to the last view opened.
  3. If it cannot determine the last view opened, return to a default view. (For example, when opening a document directly from a link outside of the application.)

The Solution

To track the name of the last view page opened, add this line of code to the beforePageLoad event of all XPages that contain view controls:

sessionScope.put('lastView', view.getPageName());

To return to the last view page when closing a document, use this code:

function returnToLastView() {
	var defaultView = 'myViewPage.xsp';
	if (sessionScope.containsKey('lastView')) {
	} else {

I generally put this function in a script library and call it within the logic of the back/close and save buttons.

Other Ideas?

How have you handled this in your applications?


10 responses to “Return to the Last View Opened in XPages”

  1. Toby Samples says :

    I have used context.getHistoryUrl() in the past as a way to control the path of my application usually works pretty well.

  2. Pals says :

    good one

  3. Oliver Busse says :

    I use a cookie called “_lastView” with the page URL. The benefit might be that this information is stored in the browser, not in the HTTP session. For that you can access the last view opened when you re-login to your application.

  4. Oliver Busse says :

    @Toby: do not use the history object to return to your view because you will get in trouble if your document form reloads more than one time, e.g. if you turn it into edit mode and back and so on. If you then call go(-1) from that object you only will return to a previous state of your document form, not to the view you opened it from. 😉

  5. LeonMatthys☺ says :

    I’m on page 133 of 200 in my view shown via a view panel.
    When I implement this solution, upon returning to the view from closing the document, I’m back at the start of the view, page 1 of 200. What if I want to stay on the current view page?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: