Saturday 27 August 2011

Access documents javascript client object model in sharepoint 2010


Ideal way to acess all the files in a document library is by using createAllItemsQuery. Lets look at an example of the same.
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(‘yourDocumentLibraryName');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, ‘Include(Title, ContentType, File)’);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success() {
var fileUrls = “”;
var ListEnumerator = this.allItems.getEnumerator();
while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
var _contentType = currentItem.get_contentType();
if(_contentType.get_name() != “Folder”)
{
var File = currentItem.get_File();
if(File != null) {
fileUrls += currentItem.get_item(‘Title’) + ‘-’ + File.get_serverRelativeUrl() + ‘\n’; }
}
}
alert(fileUrls);
}

function failed(sender, args) {
alert(“failed. Message:” + args.get_message());
}

Ads by Google

No comments:

Post a Comment