If changing the Source
query parameter works to redirect back to the New form, why not add a script to the New form itself that rewrites the Source
query param every time the form loads?
That should make it endlessly redirect back to itself.
Or, if that affects the behavior of the Cancel button also, use the PreSaveAction
to rewrite the Source
param, so it only gets changed if the user hits the Save button.
To address your comment:
You can change the URL by setting window.location.href
directly. So you could do something like
window.location.href = window.location.origin + window.location.pathname + "?Source=" + window.location.origin + window.location.pathname;
which would reset the URL to be the URL of the New form with a Source
query param of the URL back to the New form. However, setting window.location.href
causes an immediate refresh to that new URL. So you can't do that when the page first loads, or you'll end up in an endless loop of page refreshes. You could try to do that in a PreSaveAction
function, but that wouldn't really work either because the refresh to the new URL would happen before the form got submitted, so you would never be able to actually save any new items.
However, you can use window.history.pushState()
to change the URL in the address bar without causing a page refresh.
So what you can do is set up a PreSaveAction
function on the New form that looks something like this:
function PreSaveAction() {
// build the new URL
var newUrl = window.location.origin + window.location.pathname + "?Source=" + window.location.origin + window.location.pathname;
// push it to the history, effectively changing the current URL without causing a refresh
window.history.pushState({ path: newUrl }, null, newUrl);
// return true from the PreSaveAction to let the form submit
return true;
}
Now when the form submits, the Source
param will be the New form URL and the user will end up back on the New form. The beauty of this is that the PreSaveAction
function only gets called when the user clicks the Save button, so the Source
redirect only gets added if the user is actually saving a new item, which means the Cancel button will still redirect back out to AllItems.aspx
.
Now, you have tagged your question with sharepoint-online. I have not worked much with SharePoint Online, but through reading other people's questions, it's my understanding that adding Javascript to Modern pages in SharePoint Online isn't so straightforward, so I'm not sure what you would have to do to actually set up a PreSaveAction
function on your New form page. But that's what I would try to do to achieve the result you want.