Short : How to make a default SharePoint NewForm.aspx add an item and stay on the input page instead of returning to the list view AllItems?
long :
I am asked to allow SharePoint users to enter many new items to a SharePoint list without having to return to AllItems each time they save.
This requirement applies to many lists so I don't want to develop specific code for every possible content type.
I though I could write a short js file and use it whenever the the user wants to save and continue.
So far I tried to add the "Save and continue" behavior in a Content Editor Web Part (CEWP).
My first approach was to change the Source= parameter with no success (either cancel returned also to the new source or the source was ignored when set in preSaveAction().
Then I tried to set the form action in preSaveAction :
<script type="text/javascript">
JSRequest.EnsureSetup();
var src = JSRequest.QueryString["Source"];
var exit = JSRequest.QueryString["exit"];
var isDialog = JSRequest.QueryString["IsDlg"];
var dlgParam = "";
if (isDialog)
dlgParam = "&IsDlg="+isDialog;
//If exit is defined its keeping default source parameter
if (exit) {
document.forms.aspnetForm.action= location.pathname+"?Source="+exit+dlgParam;
}
function PreSaveAction(){
//before saving the original source is saved in exit
exit=JSRequest.QueryString["Source"];
src=escapeProperly(window.location.protocol + '//' + window.location.host + _spPageContextInfo.serverRequestPath);
document.forms.aspnetForm.action=location.pathname+"?Source="+src+"&exit="+exit+dlgParam;
return true;
The action is updated as expected but the user is still redirected to AllItems.aspx when a new item is saved.
Another attempt was to add a specific button reusing the SharePoint javascript action used in the ribbon and page buttons
<script type="text/javascript">
function adressePage() {
return window.location.protocol + '//' + window.location.host + _spPageContextInfo.serverRequestPath;
}
function saveContinue() {
document.getElementById("cmdSaveContinue").disabled=true;
if (!PreSaveItem()) return false;
if (SPClientForms.ClientFormManager.SubmitClientForm("WPQ2")) {
SP.UI.Notify.addNotification("Saving ...", false);
window.location = adressePage();
} else {
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("cmdSaveContinue", "", true, "", adressePage(), false, true));
}
document.getElementById("cmdSaveContinue").disabled=false;
}
</script>
<input id="cmdSaveContinue" onclick="javascript:return saveContinue();" type="button" value="Enregistrer et continuer" style="top: -5.5em; position: relative;"/>
This way, form validation is processed and the item is saved.
If error messages are returned, the form stays on NewItem but the error messages are lost after the window.location=... is executed.
When everything works well, the item is saved and the user is on a new empty NewForm.aspx.
But SharePoint SPClientForms.ClientFormManager.SubmitClientForm("WPQ2") is executed asynchronously and sometimes (not always) the redirection to
AllItems occurs after the end of my redirection.
I am stuck at this point and fear to be forced to edit each and every NewForm page in SPDesigner whenever a list is added...
Infos : SharePoint server 2013 on premise, SPDesigner possible