- You are saving a new record and want the user to be redirected back to the view item form to review the item. This will allow the user to start a workflow or perform another action on the ribbon without the need to find the item in the list or library view.
- Need the solution to use JSOM (client side script) only.
- Should work in Office 365, SharePoint Online, and On-Prem.
- By default, SharePoint redirects you to the List View you most recently came from.
- There is no out of the box setting for this.
- We solve this by adding a redirect script to each list view page that does the following:
- Check if a new item was added by the current user.
- Check and create a short term cookie to determine if they have already been redirected for this item
- Redirect the user if they haven't already been
- First add the following Javascript file to your SiteAssests/js folder.
- Create the "js" folder if it's not already there.
- Replace the ALLCAPS hard coded values with appropriate values.
SiteAssets/js/SP.RedirectOnAddItem.js
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>