locked
How to get all the files inside the folder in list RRS feed

  • Question

  • Hi Everyone
    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.

    Monday, September 22, 2014 2:57 PM

Answers

  • Hi,

    The code below returns all the names of files in a library without folders for your reference:

    public static void getAllFilesInAllFolders()
    {
        ClientContext clientContext = new ClientContext("http://sp");
        Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Lib01");
        clientContext.Load(spList);
        clientContext.ExecuteQuery();
    
        if (spList != null && spList.ItemCount > 0)
        {
            Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
               @"<View Scope='RecursiveAll'>  
                    <Query> 
                       <Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where> 
                    </Query> 
                     <ViewFields><FieldRef Name='FileLeafRef' /></ViewFields> 
              </View>";
    
            ListItemCollection listItems = spList.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
    
            foreach (var item in listItems)
            {
                Console.WriteLine(item.FieldValues["FileLeafRef"]);
            }
        }
    }

    You can also use CAML Designer to build the query statement if there are other requirements:

    http://karinebosch.wordpress.com/2012/02/03/caml-and-the-client-object-model/  

    Best regards,

    Patrick

    Patrick Liang
    TechNet Community Support

    • Marked as answer by Dennis Guo Tuesday, September 30, 2014 9:26 AM
    Tuesday, September 23, 2014 8:59 AM

All replies

  • Hi,

    This is a good link with example.

    http://sharepoint.stackexchange.com/questions/70185/sp2010-jsom-client-object-model-how-to-get-all-documents-in-libraries-including


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    Monday, September 22, 2014 3:08 PM
  • Other way to get this information OTB is

    >> Create a standard view for the  image library and Select Folder Section and then select "Show all items without folders"

    Export the Library to excel. You'll get the names along with other information too.

    hope this helps.


    Bistesh

    Monday, September 22, 2014 6:32 PM
  • Hi,

    The code below returns all the names of files in a library without folders for your reference:

    public static void getAllFilesInAllFolders()
    {
        ClientContext clientContext = new ClientContext("http://sp");
        Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Lib01");
        clientContext.Load(spList);
        clientContext.ExecuteQuery();
    
        if (spList != null && spList.ItemCount > 0)
        {
            Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
               @"<View Scope='RecursiveAll'>  
                    <Query> 
                       <Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where> 
                    </Query> 
                     <ViewFields><FieldRef Name='FileLeafRef' /></ViewFields> 
              </View>";
    
            ListItemCollection listItems = spList.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
    
            foreach (var item in listItems)
            {
                Console.WriteLine(item.FieldValues["FileLeafRef"]);
            }
        }
    }

    You can also use CAML Designer to build the query statement if there are other requirements:

    http://karinebosch.wordpress.com/2012/02/03/caml-and-the-client-object-model/  

    Best regards,

    Patrick

    Patrick Liang
    TechNet Community Support

    • Marked as answer by Dennis Guo Tuesday, September 30, 2014 9:26 AM
    Tuesday, September 23, 2014 8:59 AM