-1

I need to make a query against different lists in Sharepoint and each one can get some results.

However I dont know how to append one result to the next, to the next, see trimmed data variable.

function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
                    var enhanceFunctions = [
                        function(searchResultRow) {
                            return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
                        },
                        function(searchResultRow) {
                            return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
                        },
                        function(searchResultRow) {
                            return spService.AddImageMapping(searchResultRow, config.ImageFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatNumberFields(searchResultRow, config.NumberFields);
                        }
                    ];

                    var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields); 
                    var extendedSelectProperties = selectProperties.slice(); // copy array
                    var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
                    extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);

                    spService.GetAllListsFromWeb()
                        .then(function(lists){
                            var listEnumerator = lists.getEnumerator();
                            while (listEnumerator.moveNext()) {
                                var oList = listEnumerator.get_current();
                                var title = oList.get_title();
                                var id = oList.get_id();
                                if(title.indexOf("Bill Cycles")){
                                    // Get data from SP
                                    GetRelatedBillCyclesFromList(listid, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
                                    .then(function (data) {
                                        var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);                          
                                        // Add data to dataTable

                                    })
                                    .catch (function (message) {
                                        vm.Name = "Error";
                                        vm.ValidDataLoaded = true;
                                    });
                                }
                                //Do something with oList.
                            }

                            var dataTable = $(tableSelector).DataTable();
                            dataTable.clear().rows.add(trimmedData).columns.adjust().draw(); // Resize columns based on new data sizes                                                      
                            vm.ValidDataLoaded = true;

                        })

                }



function getAllListsFromWeb(){  
                        var deferred = $q.defer();
                        var context = SP.ClientContext.get_current();
                        var web = context.get_web();
                        var lists = web.get_lists();                          
                        context.load(lists);
                        context.executeQueryAsync(
                           function() {
                                $log.info("Successfully retrieved list item result");                          
                                deferred.resolve(lists);
                           },
                           function(error, errorInfo) {
                                $log.warn("Retrieving list item result failed");

                                deferred.reject(errorInfo);
                           }
                        );
                        return deferred.promise;
                    }

Update 1:

I get the following error: Cannot read property 'push' of undefined

Updated relevant code:

spService.GetAllListsFromWeb()
                        .then(function(lists){
                            var listEnumerator = lists.getEnumerator();
                            var result;
                            while (listEnumerator.moveNext()) {
                                var oList = listEnumerator.get_current();
                                var title = oList.get_title();
                                var id = oList.get_id();
                                if(title.indexOf("Bill Cycles") !== -1){
                                    // Get data from SP
                                    GetRelatedBillCyclesFromList(id, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
                                    .then(function (data) {
                                        var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);                          
                                        // Add data to dataTable

                                        trimmedData.forEach(function(item){ // loop over source array
                                            result.push(item); //append to result array
                                        });
                                    })
                                    .catch (function (message) {
                                        vm.Name = "Error";
                                        vm.ValidDataLoaded = true;
                                    });
                                }
                                //Do something with oList.
                            }

                            var dataTable = $(tableSelector).DataTable();
                            dataTable.clear().rows.add(result).columns.adjust().draw(); // Resize columns based on new data sizes                                                       
                            vm.ValidDataLoaded = true;

                        })
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • @Li357 — No. "This method does not change the existing arrays, but instead returns a new array." – Quentin Dec 05 '17 at 13:16
  • 3
    Possible duplicate of [How to extend an existing JavaScript array with another array, without creating a new array?](https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating) – Arman Charan Dec 05 '17 at 13:19
  • shame on the -1 – Luis Valencia Dec 05 '17 at 13:47
  • @ArmanCharan not duplicate, code is entirely different. – Luis Valencia Dec 05 '17 at 13:48
  • The "push of undefined" error in your second code snippet is because your `result` variable is not defined. Try initializing it as an array `var result = []`. – Arman Charan Dec 05 '17 at 13:53
  • 1
    The code in the question I cited is definitely relevant. `result.push.apply(result, trimmedData)` would work just as well. – Arman Charan Dec 05 '17 at 13:56
  • thanks @ArmanCharan the error is gone, but now it looks I have a more complex issue, when I debug I can see the items are being pushed into the result array, and until some point I see the result array has 13 elements, however when it goes out of the while loop and tries to render it in the datatable plugin, then result is empty again. Any idea? – Luis Valencia Dec 05 '17 at 14:06

1 Answers1

2

You can just loop over your array and add each item to the array you want to append to.

let result = [];
...

trimmedData.forEach(function(item){ // loop over source array
  result.push(item); //append to result array
});
...
Nikhil Ranjan
  • 994
  • 12
  • 16
  • please check the Update, having issues – Luis Valencia Dec 05 '17 at 13:47
  • the error is gone, but now it looks I have a more complex issue, when I debug I can see the items are being pushed into the result array, and until some point I see the result array has 13 elements, however when it goes out of the while loop and tries to render it in the datatable plugin, then result is empty again. Any idea? – Luis Valencia Dec 05 '17 at 14:07
  • You need to use promises instead of while loop to get data. Inside while you are doing a async call which will run after bottom datatable code. – Nikhil Ranjan Dec 05 '17 at 19:06