Redirect users after cancelling their list input

In my previous blog I showed how to redirect a user after saving a newly created or edited list item, by using the Source parameter. A reason as of why you would want to redirect your users could be to confirm that their input has been successfully saved or to make sure that they don’t land on your list view. Whatever the reason of the redirect may be, it’ll also redirect the user to that URL whenever he hits the cancel button. While this side effect can be useful at times, it can also be very confusing.

Imagine what happens when you redirect your users to a success-page, only does the user not hit the save button but the cancel button in stead? Right, it’ll still redirect the user to the success-page! Luckily, there is a method to redirect users to a different page whenever he hits that cancel button. This method only works on SharePoint on premises however.

To send a user to a different location when he wants to cancel his input on either the newform or editform, you’ll have to insert a Script Editor Web Part onto the page. To do this, go to the newform or editform and edit the page. Next, go to category Media and Content and select the Script Editor part. Click Add.

If you do not have the Script Editor available, you’ll have to enable it in the admin center. You can also choose to insert use option Embed Code in stead. The Embed Code option is placed onto the insert-tab of the ribbon.

Insert the next script into the web part.

<script>

var querystring = window.location.search.substring(1); 
var parameters = querystring.split("&");
var QueryString = Array();
for (var i=0;i<parameters.length;i++)
{
  QueryString[parameters[i].split("=")[0]] = parameters[i].split("=")[1];
}
if (QueryString["CancelDestination"] != undefined)
{
  var inputs = document.getElementsByTagName("INPUT");
  for (var i=0; i<inputs.length; i++)
  {
    if (inputs[i].value == "Cancel")
    {
      inputs[i].onclick = function () {document.location.href = unescape(QueryString["CancelDestination"])};
    }
  }
}
</script>

Repeat the same step for the other form (newform or editform). Same way as the Source parameter works, you now can also insert the cancel redirect destination in the URL. To do this you’ll have to insert the parameter CancelDestination= at the end of the URL. For example:
https://yoursharepointurl/yoursite/Lists/yourlist/newform.aspx?Source=https://yoursharepointurl/othersite/SitePages/success.aspx&CancelDestination=https://yoursharepointurl/yoursite/SitePages/cancelled.aspx.

More info about using the source parameter to redirect users after saving their form input? Check out my previous blog Redirect user after inserting or editing a list item.

Redirect user after inserting or editing a list item


Thanks to Techtrainingnotes.

Leave a Reply

Your email address will not be published. Required fields are marked *