Delete all list items with jsom
By Anatoly Mironov
Today I needed to “clean” a list, meaning to remove all list items. For some time ago I wrote a post about different ways of removing list items in bulk: Server Object Model, SPLinq and RPC. This time I had only the web browser. So I tried the jsom way. By the way, the javascript documentation for jsom on msdn is getting really good. Don’t miss that: How to: Complete basic operations using JavaScript library code in SharePoint 2013. Now here comes theworking code I used to remove all items in my list:
var ctx = SP.ClientContext.get\_current(),
list = ctx.get\_web().get\_lists().getByTitle('MyList'),
query = new SP.CamlQuery(),
items = list.getItems(query);
ctx.load(items, "Include(Id)");
ctx.executeQueryAsync(function () {
var enumerator = items.getEnumerator(),
simpleArray = \[\];
while (enumerator.moveNext()) {
simpleArray.push(enumerator.get\_current());
}
for (var s in simpleArray) {
simpleArray\[s\].deleteObject();
}
ctx.executeQueryAsync();
});
Enjoy
Comments from Wordpress.com
Tim Scharinger (@timswe86) - Jul 1, 2014
Thank you for sharing Anatoly! Worth mentioning is that the problem and solution probably works for most enumerators in SharePoints. For example if you would like to remove all sub sites.
Anatoly Mironov - Jul 1, 2014
Thank you for the feedback, Tim! Great to hear from you.