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.

1
  • 3
    C#, JavaScript, CSOM, SSOM...? Please be more specific Sep 23, 2014 at 12:48

2 Answers 2

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);
        }
    }
}
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>
4
  • I am sorry. May be i havent mentioned earlier. i need a code using C# client object model rather than Java script. Sep 23, 2014 at 8:24
  • You mean dotnet client object model
    – ateet
    Sep 23, 2014 at 8:39
  • yes dotnet client object model Sep 23, 2014 at 8:51
  • add new answer with dotnet client object model
    – ateet
    Sep 23, 2014 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.