Hybris95 Hybris95 - 3 months ago 51
Javascript Question

Redirect after form Submit (CSR)

Within a SharePoint form overriden by CSR (Client Side Rendering).

I tried adding a new button which does pretty much the same as the Save button except that it redirects to another form with given parameters.

The thing is, the redirection does not work.
I tried redirecting by changing the "action" property of the form but it doesn't seem to be taken in count.

Here is the new button :

<input id="custom_addLine" type="button" name="custom_addLine" value="+" class="ms-ButtonHeightWidth">


Here is the function called by the button and the addLine method following :

$('#custom_addLine').click(function(event){
event.preventDefault();
addLine(getQueryStringParameter('ID'));
});


function addLine(id) {
if(!PreSaveItem()) {
return false;
}
var actionUrl = "/Lists/PurchaseRequestLine/NewForm.aspx?PurchaseRequestID="+ id;
var encodedActionUrl = encodeURIComponent(actionUrl);

var newFormAction = location.pathname + '?Source=' + encodedActionUrl;
$('#aspnetForm').attr('action',newFormAction);

if(SPClientForms.ClientFormManager.SubmitClientForm('WPQ1')){
return false;
}
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('custom_addLine', "", true, "", "", false, true));
}


getQueryStringParameter is a custom made function to retrieve parameters from URI (which works).

The tricky part is that I want to preserve the default action URI in case the original Save button is clicked on, which is why action parameter is modified on the fly.

Answer

You can change the Source attribute directly from the original action:

 function addLine(id) {
    if(!PreSaveItem()) {
        return false;
    }

    var oldActionUrl = $('#aspnetForm').attr('action');
    var oldSource = GetUrlKeyValue("Source", true, oldActionUrl);

    var newSource = "/Lists/PurchaseRequestLine/NewForm.aspx?PurchaseRequestID="+ id;

    var newActionUrl = oldActionUrl.replace(oldSource, encodeURIComponent(newSource));

    WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('custom_addLine', "", true, "", newActionUrl, false, true));
}