Question

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);
}

});


Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top