2

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() {
    getListDetails()
}, "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();
camlQuery.set_viewXml(
    '<View><Query><Where><Eq><FieldRef Name=\'ID\' /><Value Type=\'Integer\'>' + id.toString() + '</Value></Eq></Where></Query></View>'
);
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);
clientContext.executeQueryAsync(
    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?

2

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(listItem.get_item('Title'));
                } 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);
    clientContext.load(items);
    clientContext.executeQueryAsync(
        Function.createDelegate(this,
            function () { deferred.resolve(items); }),
        Function.createDelegate(this,
            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 '16 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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