var siteUrl =
'/sites/SITENAME';
function
createCookie(name,value,minutes) {
if (minutes) {
var date = new Date();
date.setTime(date.getTime()+(minutes*60*1000));
var expires = ";
expires="+date.toGMTString();
}
else var expires = "";
document.cookie =
name+"="+value+expires+"; path=/";
}
function
readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c =
c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return
c.substring(nameEQ.length,c.length);
}
return null;
}
function
eraseCookie(name) {
createCookie(name,"",-1);
}
function
RedirectOnAddItem() {
var
clientContext = new SP.ClientContext(siteUrl);
var
web = clientContext.get_web();
this.currentUser
= web.get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(
Function.createDelegate(this,
onUserQuerySucceeded),
Function.createDelegate(this,
onQueryFailed)
);
}
function
onUserQuerySucceeded(sender, args) {
var email = this.currentUser.get_email();
var userid = this.currentUser.get_id();
var loginName =
this.currentUser.get_loginName();
var clientContext = new
SP.ClientContext(siteUrl);
var
oList = clientContext.get_web().get_lists().getByTitle('LISTORLIBRARYNAME');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef
Name = "ID" Ascending =
"FALSE"/></OrderBy><Where><Eq><FieldRef
Name="Author" LookupId="True"/><Value
Type="User">' + userid +
'</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
this.collListItem =
oList.getItems(camlQuery);
clientContext.load(this.collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onListQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
}
function
onListQuerySucceeded(sender, args) {
var
listItemInfo = '';
var
listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem =
listItemEnumerator.get_current();
listItemInfo += '\nID: ' +
oListItem.get_id();
listItemInfo += '\nAuthor: ' +
oListItem.get_item("Author").get_lookupValue();
var diff = Math.abs(Date.now()) - new
Date(oListItem.get_item("Created"));
var minutesPassed =
Math.floor((diff/1000)/60);
listItemInfo += '\nCreated (Minutes
Ago): ' + minutesPassed ;
if (oListItem && minutesPassed
< 2) {
//alert(listItemInfo.toString());
if
(!(readCookie('PANnewID') == oListItem.get_id().toString())){
createCookie('PANnewID',
oListItem.get_id().toString(), 2);
window.location
= "/sites/SITENAME/Lists/LISTORLIBRARYNAME/DISPLAYFORMNAME.aspx?ID=" +
oListItem.get_id();
}
}
}
}
function
onQueryFailed(sender, args) {
alert('Request
failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
- Finally, add the following code to a Script Editor web part on each List View page where the user would normally be redirected.
<script
src="/sites/SITENAME/SiteAssets/js/SP.RedirectOnAddItem.js"></script>
<script
type="text/javascript">ExecuteOrDelayUntilScriptLoaded(RedirectOnAddItem,
"sp.js");</script>