2

I'm looping through a number of sites to run a query that checks whether new content has been added to document libraries in that site in the previous three days. However, I'm having problems. When I get to the success callback, it is always on the last doc library, site context in that list. Is there something I'm missing here?

var queryItems = function(myContext, myDocName, mySelector){
    function getNewContent(x, y, z) {
        var myContext = x,
            myDocName = y,
            mySelector = z;
        function success() {
            if( items.get_count() > 0 ) {
                $(mySelector).find(".newContent").show();
            }
        }
        function fail() {
            console.log("Failed loading new content");
        }

        var myCtx = new SP.ClientContext(myContext),
            myList = myCtx.get_web().get_lists().getByTitle(myDocName),
            myQuery = new SP.CamlQuery(),
            date = new Date(),
            ISODate = ISODateString(new Date( date.getFullYear(), date.getMonth(), date.getDate() - 3 ) );
            myQuery.set_viewXml('<View><Query><Where><Gt><FieldRef Name="Created" /><Value Type="DateTime">' + ISODate + '</Value></Gt></Where></Query></View>');
            this.items = myList.getItems(myQuery);
            myCtx.load( this.items );
            myCtx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));
        };
        return getNewContent(myContext, myDocName, mySelector);
    }           

    for ( var obj in objDocLibs) {
        (function(datum, sel) {
            queryItems(context, datum, sel);
        })(objDocLibs[obj].name, selector)
    }
}
  • I have updated to use a callback in the for loop, however, It is still returning 0 for the number of items. – jonvines Mar 20 '13 at 12:42
2

I managed to get this working using promises, code below:

var queryItems = function(myContext, myDocName, mySelector) {
    var dfd = $.Deferred( function() {
        var inContxt = myContext,
            inDoc = myDocName, 
            myCtx = new SP.ClientContext(inContxt),
            myList = myCtx.get_web().get_lists().getByTitle(inDoc),
            myQuery = new SP.CamlQuery(),
            date = new Date(),
            ISODate = ISODateString(new Date( date.getFullYear(), date.getMonth(), date.getDate() - 3 ) );

        myQuery.set_viewXml('<View><Query><Where><Gt><FieldRef Name="Created" /><Value Type="DateTime">' + ISODate + '</Value></Gt></Where></Query></View>');
        var items = myList.getItems(myQuery);
        myCtx.load( items );
        myCtx.executeQueryAsync(
            function() { 
                var itemCount = items; 
                dfd.resolve(itemCount);
            },
            function() {
                dfd.reject(args.get_message());
            }
        );
    });
    return dfd.promise();
};

for ( var obj in objDocLibs) {
    (function(datum, sel, con) {
        queryItems(con, datum.name, sel).done( function(myItems) {
            if( myItems.get_count() > 0 ) {
                $(sel).find(".newContent").show();
            }
        });
    })(objDocLibs[obj], selector, context)
}

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.