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
