Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
Hybris95 Avatar asked Aug 23 '16 15:08

Hybris95


2 Answers

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));
}
like image 90
aprovent Avatar answered Oct 10 '22 00:10

aprovent


Please remove event.preventDefault(); from your code, it is responsible for redirection does not work

$('#custom_addLine').click(function(event){    
addLine(getQueryStringParameter('ID'));
});
like image 39
Sandip - Frontend Developer Avatar answered Oct 10 '22 00:10

Sandip - Frontend Developer