Here is the scenario that PirateEric outlines, using SPServices and jQuery.
Prepare:
- A SharePoint custom list called PolicyAccepted with two text fields, i.e. Title and UserName.
- a document library with two web part pages, PolicySplash.aspx and PolicyAccepted.aspx.
The page PolicySplash loads the script at the top, in the first web part on the page, via script link. If the script is loaded in the footer web part, the page content will briefly show before the redirect, which is not desired.
The page PolicySplash also contains a button that is rendered from the following html:
<button onclick="PolicyButtonClick()" type="submit">I accept the policy</button>
Here is the script:
<script language="javascript" type="text/javascript" src="/path/jquery-1.10.2.min.js"></script>
<script language="javascript" type="text/javascript" src="/path/jquery.SPServices-2013.02a.min.js"></script>
<script type="text/javascript">
// 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/PolicyAccepted.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 = "<Query><Where><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + userName + "</Value></Eq></Where></Query>";
$().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/PolicyAccepted.aspx";
}
</script>
Edit. I wrote this up in a blog post.