18

Could you tell me, please, how can I wait for an async query to SharePoint list in PreSaveAction()? Is it possible?

Thanks in advance!

1 Answer 1

27

In this situation I usually disable click event on default form save buttons (as long as it is sharepoint default form) and call my async functions instead. When the job is done, I call save function myself. It works well.

Example:

var originalSaveButtonClickHandler = function(){};

$(document).ready( function () {
  var saveButton = $("[name$='diidIOSaveItem']") //gets form save button and ribbon save button
  if (saveButton.length > 0) {
    originalSaveButtonClickHandler = saveButton[0].onclick;  //save original function
  }
  $(saveButton).attr("onclick", "ValidateForm()"); //change onclick to execute our custom validation function
});

function ValidateForm() { 
  //execute your async functions here
  var clientContext = SP.ClientContext.get_current();
  var templates = clientContext.get_site().get_rootWeb().getAvailableWebTemplates(1033, false);
  clientContext.load(templates);
  clientContext.executeQueryAsync(
    function() {
      alert("Success!");
      originalSaveButtonClickHandler();  //make sure to call this on your final success callback
    }, 
    function() {alert "Failure."}
  );
}

Cheers!

2
  • 1
    Hello @Sherbertas, It's working fine.. but I facing one concern, After executing our custom code it shows the default SharePoint behavior like show Required fields validation, etc.. Can it be handled first and then execute our code?? Thanks in advance..
    – SID
    Commented Apr 5, 2017 at 5:18
  • @SID, may be it is a result of server-side validation. Commented Jul 29, 2018 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.