I'm having a problem wrapping my head around these sharepoint async REST queries. The problem is that an async REST query isn't setting a field.value because the assignment is happening to soon. Even when postponing the assignment in document.read() and ExecuteOrDelayUntilScriptLoaded(). I've pulled the REST command from this microsoft technote. There's a lot more going on in the script, but this is it in a nutshell:

var id = 1;
var global1 = '';
ExecuteOrDelayUntilScriptLoaded(function() {
}, "sp.js");

function getListDetails(siteUrl) {

var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('mylist');

var camlQuery = new SP.CamlQuery();
    '<View><Query><Where><Eq><FieldRef Name=\'ID\' /><Value Type=\'Integer\'>' + id.toString() + '</Value></Eq></Where></Query></View>'
this.collListItem = oList.getItems(camlQuery);

    Function.createDelegate(this, this.onQuerySucceededgetListDetails),
    Function.createDelegate(this, this.onQueryFailed)

function onQuerySucceededgetListDetails(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        global1 = oListItem.get_item('Title');

$(document).ready(function() {
    myfield.value = global1;

I had considered not setting global1 as a global but within the onQuerySucceededgetListDetails() scope but I then run into other issues with the rest of the script.

What else can I do to force the assignment?

1 Answer 1


Consider using JQuery deferreds/Promises. Here is an example:

$(document).ready(function () {
        //don't exectute any jsom until sp.js file has loaded.        
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', prepareTables);

function prepareTables() {
    getItemsWithCaml('External User Account Request').then(
            function (camlItems) {
                var listItemEnumerator = camlItems.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var listItem = listItemEnumerator.get_current();
                } console.log('Completed table prparation.');
            function (sender, args) {
                console.log('An error occured while retrieving list items:' + args.get_message());

function getItemsWithCaml(listTitle) {
    //use of $.Deferred in the executeQueryAsync delegate allows the consumer of this method to write 'syncronous like' code
    var deferred = $.Deferred();
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();        
    var items = list.getItems(camlQuery);
            function () { deferred.resolve(items); }),
            function (sender, args) { deferred.reject(sender, args); }));

    return deferred.promise();

See this for more information: http://www.sharepointnadeem.com/2014/10/sharepoint-using-deferredspromises-or.html

  • I still haven't wrapped my head around JQuery deferreds/Promises, but it looks as though this is what I need. Thanks!
    – sansSpoon
    Jan 13, 2016 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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