1

Do You have working methods to redirect in SharePoint Online now? I'm trying to save item and then open Edit Form after clicking 'Save'. There were methods to do it before: link 1 link 2

but it redirects to default list view after save. Strange(!) that this code works, if there is error on page (if You don't fill required 'title' and try save - it shows warning and then reditects to page needed)

   // todo: add item id, '3' is for example
   var targetURL = '/products/Lists/Products/EditForm.aspx?ID=' + '3';

   // For Save Button
   var saveButton = jQuery("input[value='Save']");
   console.log(saveButton);
   saveButton.removeAttr("onclick");
   jQuery(saveButton).click = null;
   saveButton.click(function() {

       console.log("clicked");

       if (!PreSaveItem()) {
           console.log("!PreSaveItem");
           // return false;
       }

       if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) {
           //return false;
       }

       var oldUrl = jQuery('#aspnetForm').attr('action');

       var source = GetUrlKeyValue("Source", true, oldUrl);

       var newUrl = "";

       if (source) {
           console.log("Source key exist");
           newUrl = oldUrl.replace(source, encodeURIComponent(targetURL));
       } else {
           console.log("Source key not exists");
           newUrl = oldUrl + "?Source=" + encodeURIComponent(targetURL);
       }

       var elementName = jQuery(this).attr("name");
       WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newUrl, false, true));

       return true;

   });

Update:

Ok, I think I've found problem: If I use this string - validation works, but "Save" always redirects to list root:

if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) { 
return false; 
}

But if I comment this - form redirects as needed, but validation ("You cannot leave this blank") is ignored. this problem is related with validation: How do I trigger the default form validation without submitting the form

So we need to add custom validation because SPClientForms.ClientFormManager.SubmitClientForm('WPQ2') always returns true.

Imho for me it is easier to create custom page for adding item..

Thank You Nils for idea with guids! In my case I don't use [guid] culumn, but get latest id from list:

$.ajax({
url: webAbsoluteUrl + "/_api/web/lists/getbytitle('Products')/items?$top=1&$orderby=Title desc",
method: "GET",
headers: {
    "Accept": "application/json; odata=verbose"
},
success: function(data) {
    if (data.d.results.length > 0) {
        // here is last ID, so we can redirect to number+1
        var number = parseInt(data.d.results[0].Title, 10);
    }
},
error: function(data) {
    console.log("Error: " + data);
}

});


1 Answer 1

2

I have tested in Online and also 2016 on prem and can verify the behaviour there.

However, it seems that giving the Source absolute still works, as long as it is a target in the current SiteCollection.

8
  • Greetings Nils! The question is about that I cannot do this step: "on 'save' modify the return-query-parameter to some special page and supply the guid". It always goes to list default view instead of provided 'Source'
    – Gennady G
    Jul 2, 2019 at 13:56
  • Oh. Sorry. My mistake for not reading everything :-/ Does that happen on classic or modern or both?
    – Nils
    Jul 2, 2019 at 14:00
  • Classic NewForm
    – Gennady G
    Jul 2, 2019 at 14:00
  • 1
    if this behavoiur (redirect to list view) could not be overrided - the only way I see now is create custom html form instead of NewForm and redirect from this custom page :(..
    – Gennady G
    Jul 2, 2019 at 14:09
  • 1
    Sadly, I can reproduce the same in 2016 on prem...
    – Nils
    Jul 2, 2019 at 14:22

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.