SharePoint 2016 Logo

Recently I was looking for the solution about how to add the button to "Save and Stay on page" for SharePoint Edit Form page. I read several articles how to do it, but most of them were no good. But also I found one useful page which became the base for my solution.

On the page "Adding "Save and Stay on Page" button in Edit form?" I have found a solution to modify Save button in SharePoint edit form. And for one case it worked fine.

The idea of this method is to change "Source" parameter in the request. The "Source" contains the URL where page will be redirected.
Unfortunately, if you enter the page from context menu, you already have "Source" value. Usually, it is redirecting to "AllItems" or other List View.
I customized a bit of this code. In my version it doesn't matter whether it "Source" is filled with something or not.

Add this code to $(document).ready(function() { … } :

	var button = $("input[id$=SaveItem]");
	button.attr("value","Save and Stay on Page");
	// change redirection behavior
	button.removeAttr("onclick");	
    button.click(function() {
        var elementName = $(this).attr("name");
        var aspForm = $("form[id=aspnetForm]");
        var oldPostbackUrl = aspForm.get(0).action;

        var redirectUrl;
        if(oldPostbackUrl.indexOf('Source=') != -1) {
            var startSymbol = oldPostbackUrl.indexOf('Source=');
            redirectUrl = oldPostbackUrl.substring(0, startSymbol-1);
        }
        else {
            redirectUrl = oldPostbackUrl;
        }
        
        var newPostbackUrl = redirectUrl + "&Source=" +redirectUrl;

        if (!PreSaveItem())  return false;

        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    });

One more useful customization is to add to edit form new Save button. You should use SharePoint designer to do it. Find the code with Save Button and add the code for new button somewhere near it:

<input name="ToApprove" id="ToApprove" type="button" onclick="ExecuteBeforeSaving(); {ddwrt:GenFireServerEvent('__commit;__redirect={/srv/mysite/Lists/Orders/}')};" value="Send to approve" />

In the function "ExecuteBeforeSaving()" you can add your logic and checks. Using this function gives you more functionality than "PreSaveItem()".

There are two Save buttons in the form. That's why you should add two extra buttons. And remember that both of them must have unique IDs.