0

I have a function that is linked to an event (clicking a button on the page). Inside this function, I want to utilize the SPServices JQuery library to build the results in an array that the function should return. The key to this being that I need to collect information from my SharePoint libraries dynamically (dependent on user input on the page).

The problem i'm having is that no matter how I configure it, I cannot get the function to return the results. Logging the results to the console works inside the .SPServices block, so i'm confident it is grabbing the results from the library correctly. I just can't seem to get it to return the results within the function and subsequently the overall procedure.

Here is my code....

function getOptions() {
    var libraryName = "My Library";
    var arraytoReturn = new Array();
    $().SPServices({
        operation: "GetListItems",
        listName: libraryName,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                var titleVal = $(this).attr("ows_Title");
                // Exclude any undefined titleVal's and push values to the arraytoReturn Array
                if (!(typeof titleVal == "undefined")) {
                    arraytoReturn.push(titleVal);
                };
            });
            console.log(arraytoReturn); // Correct array is succesfully logged to the console here
            return arraytoReturn; // Logging the results of this function elsewhere in my code yields an undefined, no matter where I put the 'return'
        }
    });
    console.log(arraytoReturn); // Logs an empty array to the console. This is always evaluated first (before the above 'console.log').
};

I have also tried setting the entire "$().SPSerivces" code equal to a variable, but this also returns nothing.

Any help is greatly appreciated. Thanks! :)

  • Should also mention: Debugging in developer tools just skips past the entire SPServices section (however it is evaluated at the end, after the code has interrupted because the result of the function is undefined, which has affected another part of the process). I want it to evaluate the results then and there... – wilson88 Aug 7 '17 at 8:28
1

This is now resolved. Turns out I needed to set async to false inside the SPServices call. Working code below (in case it helps anybody else):

function getOptions() {
    var libraryName = "My Library";
    var arraytoReturn = new Array();
    $().SPServices({
        operation: "GetListItems",
        async: false, // Needed this
        listName: libraryName,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                var titleVal = $(this).attr("ows_Title");
                // Exclude any undefined titleVal's and push values to the arraytoReturn Array
                if (!(typeof titleVal == "undefined")) {
                    arraytoReturn.push(titleVal);
                };
            });
        }
    });
    console.log(arraytoReturn); // Now logs succesfully
    return(arraytoReturn); // Now returns succesfully
};
  • You may mark this as answer so that it helps others in search of a similar query – Priyaranjan KS Aug 7 '17 at 8:48
  • 1
    Don't use "async:false" because the modern browsers don't allow sync requests anymore (so it will be ignored by them). Instead, you have to learn how to deal with async functions. Look at Promise or jQuery.Deferred – AymKdn Aug 8 '17 at 7:01

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.