none
Custom Redirect Not Working RRS feed

  • Question

  • Hello everyone,

    I have an OOTB list form with a checkbox and a custom button that I am using to submit a list item. The purpose of the button is to check whether or not the checkbox is checked, and if it is, it will create a dynamic URL to where the user will be redirected after submitting the item. The function attached to this button contains the SharePoint OOTB process to submit list items: 

    if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));

    As you can see in the code above, there's a variable called newPostBackUrl, which is the dynamic URL I am building based on the condition mentioned at the beginning of the post. The issue I am having is that even though the dynamic URL is being created as expected, the redirection is not happening at all; it always redirect back to the original Source URL regardless. 

    This is the code I am using on the button:

    function SubmitRequest()
    {
    	var elementName = $("input[id$='btnSaveItem']").attr("name");
    	var newPostbackUrl;
    
    	/*Build a dynamic URL if checkbox is checked*/
    	if($("#CopyInfo").prop("checked")){
    		/*New PostBackUrl with Copy parameter*/		
    		newPostbackUrl = _spPageContextInfo.webAbsoluteUrl + "/Lists/Requests/NewForm.aspx?copy?Source=" + _spPageContextInfo.webAbsoluteUrl;
    		if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    	}
    	else{		
    		if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", "", false, true));
    	}
    }
    If anyone can provide some suggestions or point me in the right direction, that would great. Thanks in advanced.


    Fausto Capellan, Jr - SharePoint Admin

    Wednesday, February 22, 2017 4:04 PM

Answers

  • Hi Lee,

    Thank you for your reply. I was originally doing something like that, but I guess the code below wasn't taking the new Url where it says newPostbackUrl: 

    if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    I updated the code as described below with the difference of having the Url redirect before the IF statement above this line and it now works. 
    function SubmitForm()
    {
    	/*Variables*/
    	var buttonName = $(btnSubmit).attr("name");
    	var newPostbackUrl;
    	
    	/*Remove the onclick attribute from the button*/
    	$(btnSubmit).removeAttr("onclick");
    
    	/*Build new click function*/
    	$(btnSubmit).click(function(){
    		/*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?copy?Source=" + _spPageContextInfo.webAbsoluteUrl;
    		}
    		else{
    			/*New PostBackUrl to go back to Home page*/
    			newPostbackUrl = _spPageContextInfo.webAbsoluteUrl;
    		}
    
    		/*Redirect to the respective URL*/
    		window.location.href = newPostbackUrl;
    
    		/*Save list item*/
    		if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(buttonName, "", true, "", "", false, true));
    	});
    }


    Fausto Capellan, Jr - SharePoint Admin

    Thursday, February 23, 2017 7:53 PM

All replies

  • Hi,

    Here is one workaround, rewrite the URL when user click save button, here is my test code for your reference.

    <script type="text/javascript" src="/SiteAssets/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
           	SP.SOD.executeOrDelayUntilScriptLoaded(CustomSave, 'SP.js');                                   
            })
            function CustomSave() {
                $("input[id$='SaveItem']").bind("click", function () {
                	if ($("input[id$='BooleanField']").prop("checked")) {
                	window.location.href="http://sp:12001/_layouts/15/start.aspx#/Lists/Tasks/AllItems.aspx";
                	}
                });
            }
        </script>
    

    Best Regards,

    Lee


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

    Thursday, February 23, 2017 7:11 AM
  • Hi Lee,

    Thank you for your reply. I was originally doing something like that, but I guess the code below wasn't taking the new Url where it says newPostbackUrl: 

    if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
    I updated the code as described below with the difference of having the Url redirect before the IF statement above this line and it now works. 
    function SubmitForm()
    {
    	/*Variables*/
    	var buttonName = $(btnSubmit).attr("name");
    	var newPostbackUrl;
    	
    	/*Remove the onclick attribute from the button*/
    	$(btnSubmit).removeAttr("onclick");
    
    	/*Build new click function*/
    	$(btnSubmit).click(function(){
    		/*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?copy?Source=" + _spPageContextInfo.webAbsoluteUrl;
    		}
    		else{
    			/*New PostBackUrl to go back to Home page*/
    			newPostbackUrl = _spPageContextInfo.webAbsoluteUrl;
    		}
    
    		/*Redirect to the respective URL*/
    		window.location.href = newPostbackUrl;
    
    		/*Save list item*/
    		if(!PreSaveItem()) return false; if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(buttonName, "", true, "", "", false, true));
    	});
    }


    Fausto Capellan, Jr - SharePoint Admin

    Thursday, February 23, 2017 7:53 PM