none
Redirect After List Item Save RRS feed

  • Question

  • Hello everyone,

    Recently I was working on creating a custom redirect after an list item is saved. I recently tried the code from this blog: Custom redirect after creating a new Sharepoint Item, and even though I am successfully rewriting the content after the Source parameter, the redirection never happens. In my case, the content after the Source parameter will change based on one condition, but even then the redirection never works. Has anybody been lucky and got this to work?

    Here's the code from the blog post:

    $(document).ready(function() {
    
    	var button = $(“input[id$=SaveItem]”);
    	// change redirection behavior
    	button.removeAttr(“onclick”);
    	button.click(function(){
    		var elementName = $(this).attr(“name”);
    		var aspForm = document.forms[‘aspnetForm’];
    		var oldPostbackUrl = aspForm.action;
    		var currentSourceValue = GetUrlKeyValue(“Source”, true, oldPostbackUrl);
    		var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, “MyRedirectionDestination.aspx”);
    
    		if (!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, “”, true, “”, newPostbackUrl, false, true));
    	});
    });

    And this is my code:

    function SubmitForm()
    {
    	/*Remove the onclick attribute from the button*/
    	$(btnSubmit).removeAttr("onclick");
    
    	/*Build new click function*/
    	$(btnSubmit).click(function(){
    		/*Get the Name attribute of the Submit button*/		
    		var buttonName = $(this).attr("name");
    		var aspForm = document.forms['aspnetForm'];
    		var oldPostbackUrl = aspForm.action;
    		var currentSourceValue = GetUrlKeyValue(“Source”, true, oldPostbackUrl);
    		var newPostbackUrl;
    		
    		/*Build a dynamic URL if checkbox is checked*/
    		if($("#CopyInfo").prop("checked")){
    			/*New PostBackUrl with Copy parameter to load the NewForm.aspx with info from previous request*/
    			newPostbackUrl = _spPageContextInfo.webAbsoluteUrl + "/Lists/Requests/NewForm.aspx?Source=" + _spPageContextInfo.webAbsoluteUrl + "/Lists/Requests/NewForm.aspx?copy";
    		}
    		else{
    			/*New PostBackUrl to go back to Home page*/
    			newPostbackUrl = _spPageContextInfo.webAbsoluteUrl + "/Lists/Requests/NewForm.aspx?Source=" + _spPageContextInfo.webAbsoluteUrl;
    		}
    		
    		/*Save list item*/
    		if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(buttonName, "", true, "", newPostbackUrl, false, true));
    	});
    }

    Any assistance will be deeply appreciated. Thanks in advanced.


    Fausto Capellan, Jr - SharePoint Admin

    Thursday, March 23, 2017 2:43 PM

All replies

  • Hi Fausto,

    The following code for your reference, add the code below into a content editor web part into your new form page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var button = $("input[id$=SaveItem]"); // change redirection behavior button.removeAttr("onclick"); button.click(function() { var elementName = $(this).attr("name"); var aspForm = $("form[id='aspnetForm']"); var oldPostbackUrl = aspForm.get(0).action; var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl); var newPostbackUrl = "";

    /*Build a dynamic URL if checkbox is checked*/ if($("#CopyInfo").prop("checked")){ /*New PostBackUrl with Copy parameter to load the NewForm.aspx with info from previous request*/ newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, _spPageContextInfo.webAbsoluteUrl + "/Lists/Requests/NewForm.aspx?copy"); } else{ /*New PostBackUrl to go back to Home page*/ newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, _spPageContextInfo.webAbsoluteUrl); } if (!PreSaveItem()) return false; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true)); }); }); </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Sunday, March 26, 2017 5:53 AM
    Moderator
  • Hi Dennis,

    While I thank you for your reply, I don't have issues with the code, but with the redirection part. I am successfully able to construct the content of the Source parameter. For example, in the line where you enter the variable newPostbackUrl, it's not performing the postback to redirect to the newly built URL. 

    The word in bold below is what's not working:

    function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)

    If anybody has had success with this using my approach, I'd like to hear.

    Thank you.


    Fausto Capellan, Jr - SharePoint Admin

    Tuesday, March 28, 2017 1:24 PM
  • hello, are you sure that execution fails exactly on this line? Try to add console.log() traces to your code, check that it really reaches this line and check what parameters are really passed there. Also check are there errors in browser developer console.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Tuesday, March 28, 2017 2:48 PM
  • Hello sadomovalex,

    I ran the browser developer console to make sure the Source parameter is being updated as expected, and it is. As for what's failing, the only thing is the redirection to the postback url on the actionUrl parameter inside the WebForm_PostBackOptions function.


    Fausto Capellan, Jr - SharePoint Admin

    Tuesday, March 28, 2017 3:22 PM
  • Hi Fausto,

    Please try to remove if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false; in you code check if it works.

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

    Or please use IE developer tool to debug your code, check whether the WebForm_DoPostBackWithOptions function execute or not.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 29, 2017 1:27 AM
    Moderator
  • Hi Dennis,

    That makes it even worse; a lot of fields are not saved in the item and the redirection still doesn't work. I'm basically out of ideas. Thanks for your suggestion though.


    Fausto Capellan, Jr - SharePoint Admin

    Wednesday, March 29, 2017 1:53 PM
  • Hi,

    As a workaround, we can customize a save item button, then using jQuery Ajax with SharePoint REST API to add the list item to list. In the success method of the Ajax request, we can redirect to another page using this:

    window.location.href="your page url";

    SharePoint 2013: Working with REST API using jQuery Ajax

    https://social.technet.microsoft.com/wiki/contents/articles/31995.sharepoint-2013-working-with-rest-api-using-jquery-ajax.aspx

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 30, 2017 2:19 AM
    Moderator
  • did you check this forum thread: How do i add a redirecting link on a save button? It mentions the same code as from the article you use and there are some suggestions. Also did you try to run this code as is without modifications:

    $(document).ready(function() {
    
    var elementName;
    var oldPostbackUrl;
    var currentSourceValue;
    var newPostbackUrl;
    
    var button = $("input[id$=SaveItem]");
    
    // change redirection behavior
    button.removeAttr("onclick");
    button.click(function() {
      var elementName = $(this).attr("name");
      var aspForm = $("form[id=aspnetForm]");
      oldPostbackUrl = aspForm[0].action;
      currentSourceValue = GetUrlKeyValue("Source", true,    
      oldPostbackUrl);
      newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "/Pages/default.aspx");
    
    
      if (!PreSaveItem()) return false;
    
      WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    
    
    }); //end button.click
    
    
    }); //end doc ready
    People in mentioned forum thread said that it worked for them. Try to run it as is.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Thursday, March 30, 2017 2:51 PM
  • Hi sadomovalex,

    Yes, I did. If I need to redirect to one specific page, I can just append the Source parameter to the link where the users open the form from and they will be redirected back to that page without the need of code. However, my case is they will be redirected to either of two pages based on a condition on the list form. 


    Fausto Capellan, Jr - SharePoint Admin

    Thursday, March 30, 2017 3:00 PM
  • well as a workaround you may implement logic for saving new list item with specified metadata by yourself via javascript object model (without call to WebForm_DoPostBackWithOptions() at all) and then just set location.href to the needed url. But of course it will require some work (you will need to get values from all UI elements on the form by yourself and store them to the metadata of the created item).

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Tuesday, April 4, 2017 2:15 PM
  • Hi sadomovalex,

    I agree. I'm going to follow Dennis' suggestion above, which is to use REST API. I'll circle back to this post later since I just got involved in a migration project.

    Thanks.


    Fausto Capellan, Jr - SharePoint Admin

    Tuesday, April 4, 2017 2:31 PM
  • You can use Client-side rendering (JS Link) to customize the View/Add/Edit form list.

    Reference: https://code.msdn.microsoft.com/office/Client-side-rendering-JS-2ed3538a

    Using CSR, OnPostRender event, you can override custom SAVE button:

    https://social.msdn.microsoft.com/Forums/office/en-US/f0ee4e38-26d0-40fb-8800-97d31ce1c8bf/presaveaction-redirect-url-if-validation-success-or-return-true?forum=sharepointdevelopment#b4ed7545-4e5f-4907-884f-222a0d74addd


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Friday, April 7, 2017 8:19 AM