SharePoint: Redirect if user clicked a button previously

I just came across this question in sharepoint.stackexchange.com. When a user visits a SharePoint site, they are presented with a splash screen and need to accept the policy before they can proceed. Upon subsequent visits, the splash screen does not show, because SharePoint will remember the  user.

PirateEric outlined a possible solution: Use a SharePoint list to save the user name when the button is clicked. When the page loads, look up the user in the list. If they already exist, redirect the page, if not, show the splash page with the button to accept the policy. If the policy changes and users need to be made aware of that, simply remove all items in the list that tracks the users. All this can be done with jQuery and web services.

That intrigued me and I had a go at actually building this, using Marc Anderson’s SPServices.

How to set it up

Create a SharePoint custom list with two fields, Title and UserName. The former is the out of the box field, the latter is a simple text field.

Create two pages, the Splash page with the button and the page that is the desired destination page for all visitors. In my sample these are called Splash.aspx and MainContent.aspx

On the Splash page the code that you can see below will be loaded before any other web part. If you use a Content Editor Web Part to load the code with a content link, make sure that it’s the first web part on the page.

In many cases, JavaScript and jQuery will be placed in the last web part of the page and run after the DOM has loaded. But in this case this would mean that the Splash page appears briefly, even if it is followed by a redirect to a different page.

The Splash page provides the policy (or terms and conditions) that the user must accept, and a link or a button that the user can click to accept. This link or button must then trigger a JavaScript function. That is very easy to do. I used a button and put the html straight into a CEWP like this:

<button onclick="PolicyButtonClick()" type="submit">
 I accept the policy
 </button>

So the user clicks the button and the JavaScript function PolicyButtonClick() will run. This function can be found in the code below.

First, the jQuery library and the SPServices library are loaded. Then the user name of the current user is retrieved with the SPServices call using SPGetCurrentUser. It returns a text string in the format DOMAINAccount.  Next, the SPServices call uses the GetListItem. In the call, a CAML query is constructed that will return only list items where the column UserName equals the current user.  The items returned by that query are then counted. Since the user account is a unique value, we can safely assume that the query will either return one result or no result at all.

If the query returned an item, this means that the user has previously accepted the policy and the script will redirect to the MainContent.aspx page.  If the query did not return anything, the current page will continue to be displayed.

When the user clicks the button to accept the policy,  the user name is written into a variable. Then the SPServices operation to UpdateListItem is called and will create a new item in the list “PolicyAccepted”, storing the previously established account name in the column UserName. Then the MainContent.aspx page is loaded.

The next time the user opens the Splash page, their account name will be found in the PolicyAccepted list and they will not see the Splash page again, unless the entry in the list is deleted.

Here is the complete script:

<a href="/path/jquery-1.10.2.min.js">/path/jquery-1.10.2.min.js</a>
<a href="/path/jquery.SPServices-2013.02a.min.js">/path/jquery.SPServices-2013.02a.min.js</a>

// start the code even before the DOM is loaded, so not waiting for document ready
//$(document).ready( function() {
// get the user name
var userName= getUserName();
// find the user name in the list
var userAccepted = matchUserName(userName);
if (userAccepted == 1 )
{
// redirecting page
window.location.replace("http://path/Documents/MainContent.aspx");
}
//});

function getUserName() {
var thisUserAccount= $().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});
return(thisUserAccount);
}

function createNewItem(theTitle, theUser) {
$().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "New",
listName: "PolicyAccepted",
valuepairs: [["Title", theTitle], ["UserName", theUser]],
completefunc: function(xData, Status) {
}
});
}

function matchUserName(userName) {
var queryText = "" + userName + "";
$().SPServices({
operation: "GetListItems",
listName: "PolicyAccepted",
async: false,
CAMLQuery: queryText,
completefunc: function (xData, status) {
itemCount = $(xData.responseXML.xml).find("rs\:data, data").attr("ItemCount");
}
});
return(itemCount);
}

function PolicyButtonClick() {
var userName= getUserName();
var theTitle= "Accepted";
createNewItem(theTitle, userName);
window.location.href = "http://path/Documents/MainContent.aspx";
}

Enjoy.

3 comments

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s