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

Follow Us :

Summary : Get Files from a Folder Ecmascript\Javascript client object model SharePoint 2010,Use set_folderServerRelativeUrl to get all files from a Specific folder in SharePoint,Get Files from a Folder Javascript client object model SharePoint 2010

An example of how to Get Files from a Folder using Ecmascript\Javascript client object model in SharePoint 2010
<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();
query.set_folderServerRelativeUrl(‘/Shared%20Documents/TestFolder’);
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 in Test Folder</a>​​​

Related Post :
Get all documents from document library Ecmascript\Javascript Client Object model

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


Tags: SharePoint 2010