If you’ve ever needed to extract a list of files from a OneDrive or SharePoint folder โ including file names and sizes โ but found no export option, this guide is for you. With a bit of JavaScript and your browser’s Developer Tools, you can retrieve this metadata directly from the page.
๐ง Prerequisites
- A modern browser (Chrome, Edge, or Firefox | Tested on Brave)
- Access to the OneDrive or SharePoint folder in question
- Basic comfort using the browser’s Developer Tools (DevTools)
๐ Step-by-Step Guide
1. Open the Folder in OneDrive or SharePoint
Navigate to the folder whose file metadata you want to extract. Wait for the page to fully load. Important! Scroll down to the very bottom of the folder so that all files have been loaded.
2. Open Developer Tools
- Right-click anywhere on the page and select Inspect, or press
F12. - Go to the Console tab.
3. Run the Metadata Extraction Script
Paste the following script into the console and press Enter:
(function() {
const itemsMap = window["$spartan"]?.stores?.listItemStore?._items;
if (!itemsMap || !(itemsMap instanceof Map)) {
console.warn("โ Could not access the items Map from $spartan.");
return;
}
const rows = [];
for (const [key, item] of itemsMap.entries()) {
const fileName = item?.FileLeafRef;
const fileSize = item?.FileSizeDisplay;
if (fileName && fileSize) {
rows.push({
FileLeafRef: fileName,
FileSizeDisplay: fileSize
});
}
}
if (rows.length === 0) {
console.warn("โ ๏ธ No valid file items found in the Map.");
return;
}
const csvHeader = "FileLeafRef,FileSizeDisplay\n";
const csvRows = rows.map(row =>
`"${row.FileLeafRef.replace(/"/g, '""')}","${row.FileSizeDisplay}"`
);
const csvContent = csvHeader + csvRows.join("\n");
const blob = new Blob([csvContent], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "onedrive_file_list_from_spartan.csv";
a.click();
URL.revokeObjectURL(url);
console.log(`โ
Downloaded ${rows.length} items to CSV.`);
})();
4. Download the CSV
Once the script runs, it will automatically download a file named onedrive_file_list.csv containing the file names and sizes.
๐งช Optional: Preview File Data Before Downloading
If you’d like to preview the first few entries before downloading:
const itemsMap = window["$spartan"].stores.listItemStore._items;
let count = 0;
for (const [key, item] of itemsMap.entries()) {
if (item?.FileLeafRef && item?.FileSizeDisplay) {
console.log(`๐ ${item.FileLeafRef} โ ${item.FileSizeDisplay}`);
if (++count >= 5) break;
}
}
Notes:
The specific object that stores the list of files for the active folder is window[“$spartan”].stores.listItemStore
Another approach to retrieving the desired metadata was to use the following code to capture the requests made for folder list data:
(function() {
const capturedRows = [];
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(method, url) {
this._url = url;
return originalOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function() {
this.addEventListener('load', function() {
if (this._url && this._url.includes("RenderListDataAsStream")) {
try {
const json = JSON.parse(this.responseText);
const rows = json?.ListData?.Row || [];
rows.forEach(row => {
if (row.FileLeafRef && row.FileSizeDisplay) {
capturedRows.push({
FileLeafRef: row.FileLeafRef,
FileSizeDisplay: row.FileSizeDisplay
});
}
});
console.log(`Captured ${capturedRows.length} items so far.`);
} catch (e) {
console.warn("Failed to parse JSON from:", this._url);
}
}
});
return originalSend.apply(this, arguments);
};
window.downloadCapturedCSV = function() {
if (capturedRows.length === 0) {
console.warn("No data captured yet. Scroll through the folder first.");
return;
}
const csvHeader = "FileLeafRef,FileSizeDisplay\n";
const csvRows = capturedRows.map(row =>
`"${row.FileLeafRef}","${row.FileSizeDisplay}"`
);
const csvContent = csvHeader + csvRows.join("\n");
const blob = new Blob([csvContent], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "onedrive_file_list.csv";
a.click();
URL.revokeObjectURL(url);
console.log("โ
CSV download triggered.");
};
console.log("โ
XHR interceptor is active. Scroll through the folder to capture data.");
console.log("๐ฅ When you're done scrolling, run `downloadCapturedCSV()` in the console to save the CSV file.");
})();
The disadvantage to this is that, because this code can only be run after the page has loaded, some of the list data is already stored locally and only the dynamically-loaded portion of the list is captured.