Javascript – Can’t get ClientContext.executeQuery() to work in javascript

javascriptsharepoint-2010

I am getting information from a sharepoint list and then I want to use that data. The problem is that I need the data to be updated from the Sharepoint server before I use it, but I can't get executeQuery() to work. I can get executeQueryAsync() to work though. Here is my code:

// Global variables;
var context;
var web; 
var list;
var howManyItem = 0;
var allItems;
var randNums = [];

// Initializes the variables; Sets listname; Gets all items; 
function init(){

    context = new SP.ClientContext.get_current();
    web = context.get_web();

    // Enter the list name;
    this.list = web.get_lists().getByTitle('LetsTalkAdded');

    // Get item count in the query/list;
    var query = SP.CamlQuery.createAllItemsQuery();
    allItems = list.getItems(query);
    context.load(allItems, 'Include(Title)');
    context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); 
}

This works fine, but when I change the last line to:

context.executeQuery(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed)); 

It no longer works, but I can't run them asyncronously. If I do, the parts of my code that depend on that information don't work. Why doesn't the executeQuery() funcion work?

Best Answer

I have been trying something similar in using ExecuteQuery instead of the Async version. And from what I have found the non Async version is not supported in the JSOM. So you have to use the Async version.

Looking at his number 6. http://blogs.msdn.com/b/sharepointdev/archive/2011/07/19/working-with-the-ecmascript-client-object-model-jsom-in-sharepoint-2010-part-3-nikhil-sachdeva.aspx