I know this has been asked before here and I've viewed several similar questions, but I'm just not getting it and could use some assistance. I'm currently reading a file in SharePoint's library via an ajax query that is executed in clientContext.executeQueryAsync(), pretty much exactly as described in SharePoint's documentation:
$(document).ready(function(){
console.log("Javascript working");
$("#submit").click(function() {
file = readFile("/FileName.xlsx/Model/Tables('Table1')?$format=atom");
console.log(file);
});
});
function readFile(fileLocation) {
var clientContext;
var oWebsite;
var fileUrl;
clientContext = new SP.ClientContext.get_current();
oWebsite = clientContext.get_web();
clientContext.load(oWebsite);
clientContext.executeQueryAsync(function () {
fileUrl = "http://sharepoint2.bankofamerica.com/sites/HadoopAsAServicePortal/PlatformGovernance/_vti_bin/ExcelRest.aspx/Internal%20Team" + fileLocation;
$.ajax({
url: fileUrl,
type: "GET"
})
.done(Function.createDelegate(this, successHandler))
.fail(Function.createDelegate(this, errorHandler));
}, errorHandler);
}
function successHandler(data) {
// jsonData = xmlToJson(data.documentElement);
// cells = serialize(jsonData);
// pushCellsToLists(cells);
}
The problem is that, when I return the results of a successful query and print them, the result is undefined. I know this is because the query is being executed asynchronously while the console.log is not, and so the value isn't set when the console.log occurs. I also have heard that adding a callback is the solution here, but I'm not that familiar with callbacks and my attempts to implement that solution here have been unsuccessful. Can anyone offer any guidance?
executeQueryAsyncand another async query using$.ajax. In fact, the way you have it set up is with the$ajaxfunction as the success handler ofexecuteQueryAsync, so ifexecuteQueryAsyncsucceeds, you don't do anything with what is returned from the server, instead you kick off a second async call using$.ajax.