I am on a mission to retrieve every single item from all lists within a SharePoint site. Despite my efforts, the resources I have come across explain how to fetch all lists or all items from a list, not every item in all lists within a site context.
My code is provided below and while I can successfully obtain all lists, my challenge lies in retrieving the items, and not just from the last list (as it seems to be doing when tested in the console - this updated version may instead result in null errors due to recent changes).
var allInfo = "";
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
function getAllListsAndItems() {
var context = new SP.ClientContext('https://mysites.sprcompanies.com/personal/cnorman/charpractice/');
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded(sender, args) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
allInfo += " List: " + list.get_title() + "\n";
if (list.get_itemCount() > 0) {
var query = new SP.CamlQuery();
query.set_viewXml('<View></View>');
var items = list.getItems(query);
context.load(items);
context.executeQueryAsync(onSuccess, onFail);
function onSuccess(sender, args) {
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
}
function onFail(sender, args) {
console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
}
console.log(allInfo);
}
function onQueryFailed(sender, args) {
console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
The issue area appears to be here:
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
Initially, I attempted to append to allInfo
, but it kept generating 'not initialized' errors. I assumed that I was not loading the items correctly, but testing in the console revealed the item collection objects were displaying, leading me to believe it relates to the above segment.
Could using a for
loop to cycle through the items solve this? I only require the titles of each item. Even after trying both a for
and a for in
, errors persist. Hence, it appears the problem lies in how I am accessing each item (perhaps using incorrect properties). Thank you for your assistance!
Edit:
Incorporating this into the item onSuccess
block instead:
if (items.get_item("Title") == null) {
items.get_data().forEach(function(item) {
console.log(item.get_item('URL').get_description());
});
}
else {
items.get_data().forEach(function(item) {
console.log(item.get_item('Title'));
});
}
Both options retrieve the 'title' of an item regardless of whether it is a standard item or a link item. However, it solely gathers items from the last list and repeats them multiple times rather than traversing through every list.