4

I have been trying to get this to work, to no avail. I am wanting a button similar to the save button in 2013. This "Save and Stay" button would submit the form with data currently filled out for the page, but then redirect back to itself so that the user could continue to fill out the form if they would like. Below is what I have attempted.

I tried this first by just copying the save button and modifying some parameters, this just acted like the save button and would redirect as normal:

<input type="button" name="ctl00$ctl39$g_52193838_579d_4f8b_ba08_4d04594a729e$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItemAndStay" value="Save and Stay" onclick="if (!PreSaveItem()) return false;if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl39$g_52193838_579d_4f8b_ba08_4d04594a729e$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItemAndStay&quot;, &quot;&quot;, true, &quot;&quot;, window.location.href, false, true))" id="ctl00_ctl39_g_52193838_579d_4f8b_ba08_4d04594a729e_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItemAndStay" accesskey="O" class="ms-ButtonHeightWidth" target="_self">

Then I tried adding a button through javascript/jquery, and this would act like it would submit and return to the page, but the data would not get saved to the database and would disappear once the user left the page:

   var button = $("input[id$=SaveAndStay]");
// change redirection behavior
    button.click(function() {
    console.log ("button clicked");
    if (!PreSaveItem()) return false;
    SP.UI.Notify.addNotification("Saving ...", false);
    var myurl = window.location.href
    $("#aspnetForm").attr('action',location.pathname+'?Source=' + myurl);

    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSaven", "", true, "", myurl, false, true));
    return true;
    });

Does anyone have a way to do this? I have searched on the Internet to no avail.

2
  • 3
    SP is 15+ year old technology and not a SPA (Single Page Application), so it needs do a POST so the Server saves the Item, then sends you to another page. Only option is to redirect to the EditForm URL (which is then generated again by the server and will have all the correct required Hidden data etc.) Commented May 2, 2017 at 15:58
  • Would love to do as you suggest, but can't get the redirect to work. Commented May 2, 2017 at 17:38

6 Answers 6

2

I have tried this in my POC and giving you my approach if it could helps

When you see the "EditForm.aspx" url it always comes with source parameter to redirect the page after you finish the save operation.

I have updated my source parameter to the same edit url after it loads as shown in below

<<siteurl>>/Lists/test/EditForm.aspx?ID=7&Source=<<site url>>%2FLists%2Ftest%2FEditForm.aspx%3FID%3D7

After you save the item, it will remain on same edit page. We have to make sure source parameter value is same as request url.

2

I'd like to suggest a solution, which has advantages as compared with others. It's unnecessary to create custom forms, additional pages, or use InfoPath. The solution also works in modal and full-page edit forms. We just have to clone the OK button and replace the action value of #aspnetForm only when we'll click our custom button. All we need, just place following code in Content Editor or Script Editor web part on the edit form.

var btnOK = $("input[id$='SaveItem']:last");
var btnApply = btnOK.clone();
btnApply.val("Apply");
btnApply.removeAttr("onclick");
btnApply.click(function () {
    var postUrl = $("#aspnetForm").attr("action");
    $("#aspnetForm").attr("action", location.pathname + "?ID=" + GetUrlKeyValue("ID") + "&Source=" + encodeURIComponent(postUrl));

    if (!PreSaveItem()) return false;
    if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;
    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($get(WPQ2FormCtx.SubmitButtonID).name, "", true, "", "", false, true));
});
btnOK.after(btnApply);
4
  • DiscoveryX, we're trying to use your code, but when the 'Save & Stay' button is clicked, I'm getting a 'Cannot read property 'name' of null. I've tried your exact code and also via inspector WebForm_PostBackOptions($get(WebPartWPQ2.ctl00_ctl41_g_b0616c36_5bae_4a59_adc7_3c58097deb29_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem) to put in the button's ID. Commented Jan 9, 2019 at 16:20
  • Is it SharePoint 2013 or other version? If you didn't try it yet, you can open Edit Form in a new tab and check the results of WPQ2FormCtx.SubmitButtonID and $get(WPQ2FormCtx.SubmitButtonID) in the Developer Tools console that can point you to some solution.
    – DiscoveryX
    Commented Jan 15, 2019 at 8:38
  • Does it work in SharePoint Online? For me it always redirects to list default view: sharepoint.stackexchange.com/questions/265659/…
    – Gennady G
    Commented Jul 2, 2019 at 13:28
  • 1
    I know this is old, but I implemented this solution in Office 365 (using classic experience). The key to make it work is to get the form context. This is the 'WPQ2FormCtx' in the example by DiscoveryX. In my Office 365/SharePoint Online environment, I had to use "WPQ4FormCtx.SubmitButtonID" instead of "WPQ2FormCtx.SubmitButtonID"
    – guyleejr
    Commented Apr 26, 2023 at 18:07
1

I have a question in to MS about this, because the techniques we used to use for 2010 (updating the aspNetForm action), no longer seems to work in 2013. They've changed the timing somehow so the code doesn't work.

If this is something that you can't get out of, (giving up is an option I always try) I'd brute force it - I'd add a button that has nothing to do with the standard functionality, that builds up an update call (using your favorite ajax/SPServices/whatever...) and when done do a page refresh so you don't get a 'someone else editing this item' error.

0
1

This is something that can actually be done in InfoPath, with no custom coding. Open up the list that you'd like to edit the form for, and click the InfoPath logo/Customize form on the ribbon. Once inside, you'll be able to use the functions in the ribbon to add buttons to the form, customize their positioning, etc. After you place the button, double click it and hit "add rule". From there, you'll click on the "submit" rule, which submits data to the server but does not close the page. You can also add a second button to close the form without saving. I have done this exact same thing, and labeled that second button "cancel/close", so that users are aware that it does not save.

enter image description here

0
1

Easiest, but creates 2 pageloads

  • create an extra ASPX page back2form.aspx with one JavaScript statement

    document.location=document.referrer
    
  • use it in the Form source parameter ?source=back2form.aspx

1
  • This will not work in SharePoint Online Commented Nov 22, 2017 at 0:42
0

Only thing you could do (if it's really worth the work) is to create your own form that will submit. Create a new edit form, make it the default and have a PreSaveAction() that overrides the default submit action, use REST or JSOM to submit based on the sharepoint fields, and you can just have the page stay where it's at by having return false; at the end of the function.

Check this article

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.