0

I have a requirment to redirct the Save button in a form that enters information into a sharepoint 2013 list.

I have managed to redirect the cancel button with J Query;-

<script src="/_catalogs/masterpage/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript"> 
  $(document).ready(function() {        
  $('input[value=Cancel]').click(function() { window.location.replace("https://MYURL.com/Pages/PhoneNumbers.aspx?k=#k=*");});
  });
</script>

If I use the above code for my "Save" button it runs the validation and redirects to the new page but if I leave a mandatory field blank it flashes up the red text below the input box to show it musn't be left blank then redirects I want it to stay on the page if a field is left blank.

1

I just wrote an article on my blog about this issue after checking out many solutions out there and not getting the results I wanted.

I found out that SharePoint uses the function Nav.navigate (SP2013) or STSNavigate (SPO) for the redirect after form submission. So changing what this function does right before the redirect takes place seems to be the best way.

Here is one of the scripts I came up with:

function changeRedirect(options) {
    for (var i = 0, buttons = document.querySelectorAll(options.selector); i < buttons.length; i++) {
        var newOnClick = function(originalOnClick) {
            return function(){
                Nav.navigate = STSNavigate = function() {
                    window.location = options.redirectTo
                }
                originalOnClick()
            }
        }
        buttons[i].onclick = newOnClick(buttons[i].onclick)
    }
}

document.addEventListener("DOMContentLoaded", function(event) {
    changeRedirect({
        selector:   "input[value=Save]",
        redirectTo: "https://google.com"
    })
    changeRedirect({
        selector:   "input[value=Cancel]",
        redirectTo: "https://bing.com"
    })
})

More information here: http://spoodoo.com/change-default-form-button-redirect-simple-way/

  • Forgive my ignorance is this a simple case of throwing this script within the newform.asp page within a Script tag and calling the Java Script as normal I'm on version 1.10.2 – user3271150 Sep 6 '16 at 10:27
  • Yes, just copy/past everything into a Script Editor Webpart or Content Editor Webpart or into a ScriptBlock if you modify the page directly via SPD. – Daniel Stölzner Sep 6 '16 at 10:29
  • I've tried that and its not working, I noticed when I click to follow the code hyperlinks in SPDesigner for 'originalOnClick()' and 'newOnClick' that it says the hyperlink could not be found could this be indicitive of something? – user3271150 Sep 6 '16 at 10:55
  • Don't think so. Try putting it into a Script Editor Webpart and see if it works. The script as it is now is not MDS safe. So should you have MDS activated try deactivating it. I just tested it again with on-premises SP2016 and it works right away (in SEWP with MDS off). – Daniel Stölzner Sep 6 '16 at 11:49
1

Wouldn't it suffice to alter the action of the sourrounding form?

Or check what the page normally does before sending the form. Some SharePoint pages declare a WebForm_OnSubmit-function that will start the process of checking all required fields and return a boolean indicating if the validation was successful, or not.

if you, i.e. take a look at the advanced settings page of a list (/_layouts/15/advsetng.aspx) the HTML looks like this:

<html>
   <head>..</head>
   <body>
     <form id="aspnetForm" onsubmit="javascript:return WebForm_OnSubmit();" action="/sites/searchcenter/_layouts/15/advsetng.aspx?List=some-list-id" method="post">
       <!-- many cool input elements... -->
       <input onclick='javascript:someCoolPostBack()' type="button" value="OK">
     </form>
   </body>
</html>

here you can see first in the onsubmit=... of the form, the javascript function used to validate the page, so if you call var valid = WebForm_OnSubmit() you'll get the current validation-state of the page (and most probably trigger all the little red error-warnings in the page...).

What you can also see, is the action=... of the form. This is the url the postback will go to, if the form is to be submitted. So if you have a page, whose save-button needs to send the data to a different location than the one intended by the page you should try to modfy the action of the form instead of the save button.

Given the above example and using jQuery the modification is fairly simple:

$('#aspnetForm').attr('action', 'https://MYURL.com/Pages/PhoneNumbers.aspx')
  • I'm not brilliant with J Query etc. what do you mean by the 'action' of the surrounding form? – user3271150 Sep 5 '16 at 8:10
  • I updated the answer. – Nils Sep 5 '16 at 16:43
0

Define a boolean first in your Save method eg: boolsp= true;. If any validation is failing then handle it by assigning boolsp = false; At the end of function try this

    if(boolsp)
    {            
     return true;
    }
    if (!boolsp) // if any validation fails
    {
      alert("FieldsIncomplete");
      var aspForm = $("form[name=aspnetForm]");         
      $("#aspnetForm").attr('action',location.pathname+"?Source="+HomePageURL);
      return false;
    }

Note: HomePage URL should be the URL where you want the page to stay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.