0

I want to redirect a Sharepoint 2013 Edit form to Display form after click on Save button. I put the url in PreSaveAction, it doesn't work. It returns to list most time. Sometimes, it works after I edit the form a few times. But if I re-open the browser, it doesn't work again. It's so wired. See my code below.

$(document).ready(function(){ $"select[title='Department_ID']").attr('disabled','disabled'); var oc=$("input[value='Save']").attr("onclick"); $("input[value='Save']".attr('onclick', 'if(checkError()) { alert("error..");} else{' + oc +'}'); } function PreSaveAction(){ var deptID = $"select[title='Department_ID']").val(); url = "https://...../DisplayForm.aspx?ID=" + deptID; window.location.href=url; return true; } function checkError(){ /*do some checking on the other field. if 'wrong', return true else return false */ }
1

There is an easy way.

In the debug mode, we found that after data submission the ‘click’ event handler will invoke a callback named “CompleteClientRequest” which will acquire redirect info from ‘Source’ parameter of the current page URL.

Hence we could replace the current URL with our expected URL prior to the callback. SharePoint offers an out-of-box function named “PreSaveAction’, which will be called in the front when the user click save button. We can place our custom code there:

function PreSaveAction() {


        var targetURL = 'http://sp/sites/dev/SitePages/DevHome.aspx';
        var oldActionUrl = $('#aspnetForm').attr('action');

        var oldSource = GetUrlKeyValue("Source", true, oldActionUrl);
        var newActionUrl = oldActionUrl.replace(oldSource, encodeURIComponent(targetURL));            

        history.replaceState('', '', newActionUrl);

        return true;
}

BR

| improve this answer | |
  • Thank you very much!! It works great. – JJL May 12 at 19:45
0

If, you want to move to "Display Form" from "New Form", then call the NewForm.aspx like this, from a hyperlink.

NewForm.aspx?Source=DispForm.aspx

And in the DisplayForm.aspx, place a content editor script, and check whether it has the ID parameter in url, if not then reload the page with the last modified ID, by fetching that that ID

    function fetchLatestID() {
        var camlQuery = "<Query>";
        camlQuery += "<OrderBy>";
        camlQuery += "<FieldRef Name='ID' Ascending='False' />";
        camlQuery += "</OrderBy>";
        camlQuery += "</Query>";

        var latestID = 1;
        $().SPServices({
            operation: "GetListItems",
            async: false,
            listName: "ListName",
            CAMLQuery: camlQuery,
            CAMLViewFields: "<ViewFields><FieldRef Name='ID' /></ViewFields>",
            CAMLRowLimit: 1,
            CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll'/></QueryOptions>",
            completefunc: function (xData, Status) {
                $(xData.responseXML).SPFilterNode("z:row").each(function () {

                    latestID = parseInt($(this).attr("ows_ID"));

                });
            }
        });
        return latestID;

    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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