Thursday, 24 April 2014

SharePoint get all documents in libraries including all folders recursively

SharePoint get all documents in libraries including all folders recursively
function documentQuery(listName) {
var ctx = new SP.ClientContext.get_current();
var oLibDocs = ctx.get_web().get_lists().getByTitle(listName);
var caml = new SP.CamlQuery();
caml.ViewXml = "<View Scope='RecursiveAll'></View>";
this.allDocumentsCol = oLibDocs.getItems(caml);
ctx.load(this.allDocumentsCol, "Include(FileLeafRef, ServerUrl)");
ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededCallback), Function.createDelegate(this, this.onFailedCallback));
}
function onSucceededCallback(sender, args) {
var libList= "";
var ListEnumerator = this.allDocumentsCol.getEnumerator();
while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
var currentItemURL = domURL + currentItem.get_item('ServerUrl');
libList += currentItem.get_item('FileLeafRef') + ' : ' + currentItemURL + '\n';
}
alert(libList);
}
function onFailedCallback(sender, args) {
alert("failed. Message:" + args.get_message());
}