3

In my NewForm list I was trying to Redirect page after save button was clicked, and in my NewForm I have added a script editor with the following code:

$(document).ready(function() {

var button = $("input[id$=SaveItem]");
// change redirection behavior
    button.removeAttr("onclick");
    button.click(function() {
        var elementName = $(this).attr("name");
        var aspForm = $("form[name=aspnetForm]");
        var oldPostbackUrl = aspForm.get(0).action;
        var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl);
        var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "http://www.google.com");

        if (!PreSaveItem()) return false;
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    });

});

but when I click save button it shows me the following error

jquery-1.8.0.js:3061Uncaught TypeError: Cannot read property 'action' of undefined

Note: I have added jquery 1.8.0 library to my script

Thanks

1
  • Is your aspnetForm selector correct? Try $("form[name='aspnetForm']") instead. Check that it's actually returning something. The problem is aspForm.get(0) is undefined.
    – Submits
    Commented Aug 24, 2016 at 7:52

2 Answers 2

3

Really I tried this before to redirect after saving to another URL for SharePoint 2013. but unfortunately it's not working , it looks like working with SharePoint 2010 .

But to overcome this error Uncaught TypeError: Cannot read property 'action' of undefined

  • Remove var aspForm = $("form[name=aspnetForm]"); and var oldPostbackUrl = aspForm.get(0).action;

  • And set var oldPostbackUrl = window.location.href

Note : This code will replace Source query string with your redirect URL and to make sure that add alert (newPostbackUrl); after var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "http://www.google.com"); that will replace successfully but redirect in SharePoint 2013 will not working !

A workaround :

To can do that at SharePoint 2013 as a workaround

  • Open your all items page and set this code
  • Add Script Editor.
  • Add the following code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>

$( document ).ready(function() {

document.getElementById("idHomePageNewItem").removeAttribute("onclick");


document.getElementById("idHomePageNewItem").href="http://epm/workflow/Lists/Shoe%20Models/NewForm.aspx?Source=add your custom page url"; 
});

</script>

Note : At document.getElementById("idHomePageNewItem").href= put your link to NewForm page before Source Query String and after it add your Custom Page URL that you want to go back.

Also you can create an event receive on Itemadded to can redirect to your desired Url , to can create an Event Receiver Check this

6
  • I have sharepoint 2013 and it is not working
    – Iamnderon
    Commented Aug 24, 2016 at 8:12
  • after I have alerted newPostbackUrl I have the following result: home.aspxhttp://Site/Lists.empribbon/NewForm.aspx?RootFolder=&IsDlg=1
    – Iamnderon
    Commented Aug 24, 2016 at 8:16
  • this is will not work with SharePoint 2013 as I mentioned in my answer , but if you read my answer again you will know that this code tries to replace the source query string that replaced successfully with my modification and to make sure that at alert (newPostbackUrl); after var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "google.com"); but finally it will not redirect Commented Aug 24, 2016 at 8:16
  • so do you have any idea how to redirect to my desired page?
    – Iamnderon
    Commented Aug 24, 2016 at 8:17
  • I tried for a 2 hour to use all instruction related to this with no luck so I tried to use Event Receiver on Itemadded as mentioned in my updated answer this the only solution that works for me , but I didn't know if there's a simple solution rather than that or not, Commented Aug 24, 2016 at 8:26
0

var aspForm = $("form[id=aspnetForm]"); Use id instead of name

var button = $("input[id$=SaveItem]").eq(1); //target the save button in the bottom
    // change redirection behavior
        button.removeAttr("onclick");
        button.click(function() {
            var elementName = $(this).attr("name");
            var aspForm = $("form[id=aspnetForm]"); //target id not name
            var oldPostbackUrl = aspForm.get(0).action;
            var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl);
            var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "<your url should be internal>");

            if (!PreSaveItem()) return false;
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
        });

If you put external url, it fails to redirect. But works with url within the site.

Hope this help

7
  • when I click save button, it happens nothing
    – Iamnderon
    Commented Aug 24, 2016 at 8:04
  • please check my updated answer
    – Ravindra
    Commented Aug 24, 2016 at 11:41
  • Again it is not working, to test that javascript is working I set an alert for newPostbackUrl after var newPostbackUrl but even alert is not being shown, why?
    – Iamnderon
    Commented Aug 24, 2016 at 12:31
  • Did you click on the button? debug using Chrome devloper tool
    – Ravindra
    Commented Aug 24, 2016 at 13:12
  • No I cannot make it work, it is not reading
    – Iamnderon
    Commented Aug 25, 2016 at 9:26

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.