Hello, today I have one super trick to clean up lists in SharePoint online.
If you have any list in the SharePoint online which exceeded the limit of 5000 items and now you can no longer see the items or apply filters and groupings:
- Navigate to SharePoint site where list is present, for example, https://domain/sites/PWA
- Open Inspect Element by clicking F12 in any browser and open console tab.
- Copy and Paste following code in the console, inform the ListName (Display Name not Internal Name) and if you prefer to reduce the RowLimit. (The max is 5.000)
var clientContext;
var website;
var oList;
var cnt = 0;
// Make sure the SharePoint script file ‘sp.js’ is loaded before your code runs.
SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, sharePointReady);
// Create an instance of the current context.
function sharePointReady() {
clientContext = SP.ClientContext.get_current();
website = clientContext.get_web();
oList = website.get_lists().getByTitle(‘ListName‘);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(‘<View><RowLimit>5000</RowLimit></View>’);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(website);
clientContext.load(collListItem, ‘Include(Id)’);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = ”;
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var ID = oListItem.get_id();
var oListItemDel = oList.getItemById(ID);
oListItemDel.deleteObject();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onDeleteSucceeded), Function.createDelegate(this, this.onDeleteFailed));
console.log(ID + ” : Deleted”);
}
}
function onQueryFailed(sender, args) {
console.log(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
function onDeleteFailed(sender, args) {
console.log(‘Delete failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
function onDeleteSucceeded(sender, args) {
cnt = cnt + 1;
console.log(‘Delete success : ‘ + cnt);
}
You can run this code as many times as necessary.
I hope you liked!
Diego Pereira