Could you tell me, please, how can I wait for an async query to SharePoint list in PreSaveAction()? Is it possible?
Thanks in advance!
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!