Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Sharepoint 2010 ECMAScript – Get all documents from document library

05/19/2011  
Category: SharePoint 2010


Below is a code snippet to Retrieve all the Files in a Specified document library using ECMAScript\Javascript object model.
For testing purposes you can copy and paste the script in a Content editor webpart. I have used
Shared Documentsa document library for testing. The below code will only retrieve documents from the root of the document library and not from sub-folders.

<script type=”text/ecmascript”>
function ViewAllFiles()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(‘Shared Documents’);
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 += File.get_serverRelativeUrl() + ‘\n’;
}
}
}
alert(fileUrls);
}
function failed(sender, args) {
alert(“failed. Message:” + args.get_message());
}
</script>
<a href=”#” onclick=”Javascript:ViewAllFiles();”>View All Files</a>​

Related Post :
Get all Folders using Ecmascript\Javascript client object model SharePoint 2010

Get Files from a specific Folder Ecmascript\Javascript client object model SharePoint 2010

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

Set Choice Field value with value from Querystring Sharepoint Javascript
Set Choice Field value»

Here is a code snippet to get a querystring value from the url, say from any listform and populate the»

SharePoint 2010 Tutorial for beginners
SharePoint 2010 Tutorial for»

Related Post Sharepoint 2010 Object model Tutorial What is SharePoint 2010 ? Unlike, SharePoint 2007,  SharePoint 2010 is powerful and»

Get External data from offline BCS data using SharePoint 2010 object model
Get External data from»

To get the external data from the offline data cache using the BCS API, you would create an event handler»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012