5

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.

Hybris95
  • 2,286
  • 2
  • 16
  • 33

2 Answers2

4

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));
}
aprovent
  • 941
  • 6
  • 20
  • Will this still submit the form to the same location ? I mean originally SharePoint redirects after the submit using the "Source" parameter given in the URI is there any way to redirect elsewhere than where the Source parameter is pointed at ? – Hybris95 Aug 31 '16 at 10:31
  • Does the original Source parameter is stored In the original form action? When you restore the original action, the next submit will result to the same (depending of input values) – aprovent Aug 31 '16 at 10:49
  • I'm not sure to understand the problem. Does changing the form action works ? If not, my reponse can help you, otherwise is it your Source attribute that does'nt work ? – aprovent Aug 31 '16 at 10:56
  • It's more about changing the Source parameter than the Action attribute. I want the form to keep submitting like it used to but to redirect to another source. The thing is, I don't know how SharePoint native forms manage the Source parameter, if it's simply taken from URI or stored elsewhere (I could modify). – Hybris95 Aug 31 '16 at 11:40
  • Original save button had to be overriden in order the redirection to occur. I modified your answer consequencely. – Hybris95 Sep 08 '16 at 08:23
2

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

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