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.
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));
}
Please remove event.preventDefault(); from your code, it is responsible for redirection does not work
$('#custom_addLine').click(function(event){
addLine(getQueryStringParameter('ID'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With