0

I have a requirement where Users need to stay on list's NewForm.aspx after submitting the item details.

They should be able to create as many items until they click on Cancel without redirecting to AllItems.aspx

I have tried below solutions but that did not entirely help.

  1. Tried appending Source on "New Item" button but that allows users to create an item only twice.
  2. Redirecting to NewForm in PreSaveAction function before returning it to true but that will bypass column validations.

Is there anyone who has worked on a similar requirement? Any help is appreciated.

2 Answers 2

4

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 . 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.

3
  • How can I make changes to Source parameter without affecting the default Save and Cancel functionality ? I have seen lot of people saying the same and myself thought the same but could not find any solution for that so an example would be great.
    – Ravi
    Dec 12, 2019 at 16:17
  • 1
    @Ravi updated my answer with an example. Dec 12, 2019 at 20:06
  • It works like charm. Will test more and let you know in case of any issues. Thank you so much @Dylan
    – Ravi
    Dec 13, 2019 at 5:18
0

we can add the RedirectURL on existing SP button attribute:

<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton2" RedirectUrl="https://..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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