Subscribe via Feed

Avoid saving duplicate documents on browser refresh

Tommy Valand, Jun 1, 2010 5:17:56 PM

Update 22.06.10: Parameters are carried over when the redirect happens.

A reader wanted to know if there was some way to avoid duplicate documents when saving a document, refreshing the browser, and pressing ok on the resulting dialog. I wasn't able to recreate the problem of duplicate documents, but here's a workaround for the "POST" dialog. It should also take care of any duplicate documents on browser refresh.

In JSF lingo, this is known as the POST-Redirect-GET pattern. This is quite easy to accomplish in XPages.

On the save button, put the save action, and an Execute Script action. In the script part, call on the function below (put it in a SSJS Script Library/add it to the page). This makes the server redirect to the document in a get request, and you avoid the double posting problem.

function redirectToCurrentDocument( switchMode:boolean ){
try {
if( typeof currentDocument === 'undefined' || viewScope.stopRedirect ){ return; }
// Gets the name of the XPage. E.g. /Person.xsp
var page = view.getPageName();

// Finds extra parameters, strips away XPages parameters/trims leading &
var parameters = context.getUrl().getQueryString().substring(1);
var extraParameters = parameters.replace(
/(action=\w{4}Document[\&]{0,1})|(documentId=[\w]{32}[\&]{0,1})/g, '');
    extraParameters = extraParameters.replace( /\&$/, '' );

// Gets the unid of the current document
var unid = currentDocument.getDocument().getUniversalID();

// Sets/changes the action according according to the mode the document is in
var isEditable = currentDocument.isEditable();
if( switchMode ){ isEditable = !isEditable; } // false -> true / true -> false

var action = ( isEditable ) ? 'editDocument' : 'openDocument';

// Open the current document via a get request
var redirectUrl = page + '?documentId=' + unid + '&action=' + action;
if( extraParameters ){ redirectUrl += '&' + extraParameters; }
context.redirectToPage( redirectUrl );
} catch(e){ /* Exception logging */ }

2 responses to Avoid saving duplicate documents on browser refresh

Tommy Valand, June 6, 2010 11:59 AM

If you mean XPages applications in the Notes Client, I would think the code should work the same. From what I understand, XPages in the Notes Client runs in an embedded browser control.

I haven't tested it, so I can't promise anything.

FK, June 6, 2010 11:41 AM

does it work in Notes client?