4

I want to get all the images name inside the folder which is present in list.

site>list>folder>files

I want to achieve this using csom code.

please help me i am able to get the list but not folder names and files.

Thanks in advance.

Thor
  • 123
  • 6
mallela prakash
  • 67
  • 1
  • 3
  • 7

2 Answers2

13

Run this code. I have taken "filename" as a folder - you can replace it with any folder name that you want.

ClientContext cxt = new ClientContext("http://win-8c97ll22qqc:100/sites/dev");
List list = cxt.Web.Lists.GetByTitle("Documents");

cxt.Load(list);
cxt.Load(list.RootFolder);
cxt.Load(list.RootFolder.Folders);
cxt.Load(list.RootFolder.Files);
cxt.ExecuteQuery();
FolderCollection fcol = list.RootFolder.Folders;
List<string> lstFile = new List<string>();
foreach(Folder f in fcol)
{
    if (f.Name == "filename")
    {
        cxt.Load(f.Files);
        cxt.ExecuteQuery();
        FileCollection fileCol = f.Files;
        foreach (File file in fileCol)
        {
            lstFile.Add(file.Name);
        }
    }
}
Wai Ha Lee
  • 103
  • 6
ateet
  • 861
  • 5
  • 17
1

An example of how to Get Files from a Folder using Ecmascript\Javascript client object model in SharePoint

<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>
Thor
  • 123
  • 6
ateet
  • 861
  • 5
  • 17