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

Thanks in advance!

up vote 25 down vote accepted

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!

  • 1
    Works fine! Thanks you! – Alexander Ulmaskulov Sep 30 '15 at 12:44
  • 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 Apr 5 '17 at 5:18
  • @SID, may be it is a result of server-side validation. – Alexander Ulmaskulov Jul 29 at 13:37

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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