Wednesday, 5 June 2019

Ensure SPServices script load before calling it

function ensureSPServices(callbackFunction) {
if($().SPServices == null) {
console.log("$().SPServices not loaded");
// If SPServices has not been loaded, this will return as a null value and let us know we need to load the library. The jQuery
// getScript method runs asynchronously, so we use the callback function for initial calls to the service
jQuery.getScript("jquery.SPServices.js", callbackFunction);
console.log("SPServices is now loaded!!!");
} else {
// SPServices is already loaded, so execute our callback function which contains the rest of the page initialization.
console.log("SPServices already loaded");
callbackFunction.call(null, "SPServices already loaded");
}
}
function InitializePage(data, textStatus) {
// This is the callback function called by the ensureSPServices function. 
// Here is where I would place any web-service calls that happen on the loading
// of the page. 
// i.e. GetDocumentLibraryTotalCount(libraryName, countContainer);
initPage();
}
function initPage() {
console.log("start initPage");

returnedListData(onComplete);
}
function returnedListData(whenDone){
console.log("returnedListData called");
var listData;
setTimeout(function(){
 console.log("start getting list data");
 RequestId = 1;
 listData = GetListItem(RequestId, "List Name");
 console.log("done getting list data");
 return whenDone(listData);
},10);
}


        _spBodyOnLoadFunctionNames.push("ensureSPServices(InitializePage)");