1

The code below iterates subsites and finds a lists called TeamRequests in each site. At the moment it only displays the 1st item of the list. I'm trying to alter the code so it iterates all the items in TeamRequest so I can locate a particular item. In TeamRequests I have a column called AssignedTo, if this equals a value from an array (not defined in code yet) I want it to report a match. Any suggestion on how to do this pls?

SP.SOD.executeOrDelayUntilScriptLoaded(function () {
    var myCtx = SP.ClientContext.get_current();
    var root = myCtx.get_site().get_rootWeb(); // may need to do this if in a sub site
    var webs = root.get_webs(); // fetch a reference to all the sub webs
    var query = new SP.CamlQuery();
    // only need to fetch the first item returned from each Project list
    //query.set_viewXml('<View Scope="RecursiveAll"><Query></Query><RowLimit>1</RowLimit></View>');
    query.set_viewXml('<View Scope="RecursiveAll"><Query></Query></View>');
    var queryItemCollections = [];
    // make sure to load the lists data so we can check
    // if the "Projects" list exists in each site
    myCtx.load(webs, "Include(Id, Lists)");
    myCtx.executeQueryAsync(function () {
        // push new SP.ListItemCollections into an array and tell the ctx to fetch them

        console.log(webs.get_data().length);

        for (var i = 0; i < webs.get_data().length; i++) {
            var web = webs.get_data()[i], itms;
            if (checkForProjectsList(web)) {
                itms = webs.get_data()[i].get_lists()
                           .getByTitle("TeamRequests").getItems(query);

                queryItemCollections.push(itms);
                myCtx.load(itms);
            }
        }


        myCtx.executeQueryAsync(function () {
            if (queryItemCollections.length == 0) {
                console.log("collection empty");
            }

            queryItemCollections.forEach(function (item) {
                // do work with individual list items here
                console.log(item.getItemAtIndex(0).get_item("Title"));

            });
        }, genericFailHandler);
    }, genericFailHandler);


    function checkForProjectsList(web) {
        var lists = web.get_lists().get_data();
        for (var j = 0; j < lists.length; j++) {
            var list = lists[j];
            if (list.get_title() == "TeamRequests") {
                return true;
            }
        }
        return false;
    }
    function genericFailHandler(sender, args) {
        console.log(args.get_message());
    }
}, "SP.js");

UPDATE changed code, when it runs it prints out the 3 list names as expected but the While loop always shows the items from the last list, it needs to show items from all the lists.

From the 3 lists I have (one in every subsite) the code outputs the last list items 3 times. It should output List A items, List B items etc

SP.SOD.executeOrDelayUntilScriptLoaded(function () {
    var myCtx = SP.ClientContext.get_current();
    var root = myCtx.get_site().get_rootWeb(); // may need to do this if in a sub site
    var webs = root.get_webs(); // fetch a reference to all the sub webs
    var query = new SP.CamlQuery();
    // only need to fetch the first item returned from each Project list
    //query.set_viewXml('<View Scope="RecursiveAll"><Query></Query><RowLimit>1</RowLimit></View>');
    query.set_viewXml('<View><RowLimit>100</RowLimit></View>');
    var queryItemCollections = [];
    // make sure to load the lists data so we can check
    // if the "Projects" list exists in each site
    myCtx.load(webs, "Include(Id, Lists)");
    myCtx.executeQueryAsync(function () {
        // push new SP.ListItemCollections into an array and tell the ctx to fetch them

        //console.log(webs.get_data().length);

        for (var i = 0; i < webs.get_data().length; i++) {
            var web = webs.get_data()[i], itms;
            if (checkForProjectsList(web)) {
                itms = webs.get_data()[i].get_lists()
                           .getByTitle("TeamRequests").getItems(query);

                queryItemCollections.push(itms);

                myCtx.load(itms);
            }
        }


        myCtx.executeQueryAsync(function () {


            if (queryItemCollections.length == 0) {
                console.log("collection empty");
            }
            //console.log('len '+queryItemCollections.length);
            var count=0;
            queryItemCollections.forEach(function (item) {
                // do work with individual list items here

                var listItemEnumerator = itms.getEnumerator();

                  var listItemInfo = '';
                  while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo = oListItem.get_item('_ProjectRef') + '\n+++++++++\nID: ' + oListItem.get_id() + 
                '\nTitle: ' + oListItem.get_item('Title')+'\n-------';

                console.log( listItemInfo);

                } // end while

                 console.log(item.getItemAtIndex(0).get_item("_ProjectRef")); // this show correct lists
                 count++;


            });
        }, genericFailHandler);
    }, genericFailHandler);


    function checkForProjectsList(web) {
        var lists = web.get_lists().get_data();
        for (var j = 0; j < lists.length; j++) {
            var list = lists[j];
            if (list.get_title() == "TeamRequests") {
                return true;
            }
        }
        return false;
    }
    function genericFailHandler(sender, args) {
        console.log(args.get_message());
    }
}, "SP.js");
5
  • Does the code work now, at all? What's it doing? Is there an issue or are you needing to just get it over the finish line? Commented Jan 22, 2016 at 13:46
  • Code works but only give the 1st item in every list. I require every item in the list. Commented Jan 22, 2016 at 14:42
  • I see the new update. What's happening is that you're overlapping yourself on the async calls. Your queryItemCollections variable is too high up, or out, so it loops async through the webs, bang bang bang then loops through the lists bang bang bang and the last one is what is assigned to the queryItemCollections var. The var is not persistent through out. Declare the queryItemCollections in the web loop, that should help associate that object to that web instance Commented Jan 22, 2016 at 19:55
  • Hi I've moved queryItemCollections variable in the web loop. The result gives me items from the last web, missing items from other webs. Is there a way to see value of variables (itms) at runtime. I cant step through as it jumps through other libraries. Commented Jan 25, 2016 at 8:52
  • I don't think moving the queryItemCollections down helps, as now it always contains the value of 1 and then the final loop iterates once and only cycles list items for the last web. Maybe queryItemCollections need to be a 2-dimenional array? Commented Jan 25, 2016 at 9:39

1 Answer 1

0

Thanks for the help, I found the problem with the script:

  var listItemEnumerator = itms.getEnumerator();

item was needed to replace itms

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.