Hybris95 Hybris95 - 1 year ago 181
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 :


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;

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 Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download