SharePoint ECMAScript (Client Object Model) Empty List Helper

Lists are a lot more useful as data storage mechanisms in SharePoint 2010 since there are important relational features finally built into them. Coupled with the client object model features you can start to do some really nifty data manipulation with some quick, easy code. As such, I have been starting to build up a bunch of bulk list operations that I’m using in a static manner for both some internal as well as client applications.

In the below helper function, I am emptying the items out of a list. It’s actually probably better to abstract the list title to the emptyListHelper parameters so you can pass in a string in a shared manner, so that’s might be a good idea.

[js]
var siteUrl = ‘/AdamBuenz/EmptyItemsTest’;

function emptyListHelper() {
var clientContext = new SP.ClientContext(siteUrl);
var list = clientContext.get_web().get_lists().getByTitle(‘TestList’);
var camlQuery = new CamlQuery();
camlQuery.set_viewXml(“Some Value To Match Value100“;
this.listItems = list.GetItems(camlQuery);
clientContext.Load(this.listItems);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
var listItemEnumerator = this.listItems.getEnumerator();
while (listItemEnumerator.moveNext()) {
var item = listItemEnumerator.get_current();
item.DeleteObject();
}
}

function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

[/js]

Share

One thought on “SharePoint ECMAScript (Client Object Model) Empty List Helper”

Leave a Reply

Your email address will not be published. Required fields are marked *