1

I am trying to read the file names from SharePoint document library. I have uploaded some files and created some folders on the site. I get the ItemCount number. But when i try to get the File count or Folder count it shows 0. It's not traversing into the foreach loop. Any answer would be helpful. Thanks in advance.

   using (var clientContext = spContext.CreateUserClientContextForSPHost())
        {
            List list = clientContext.Web.Lists.GetByTitle("Documents");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>";
            Folder ff = list.RootFolder;

            FolderCollection fcol = list.RootFolder.Folders;
            List<string> lstFile = new List<string>();

            clientContext.Load(list.RootFolder.Folders);
            clientContext.Load(ff);
            clientContext.Load(list);
            clientContext.Load(list.RootFolder);
            clientContext.Load(list.RootFolder.Folders);
            clientContext.Load(list.RootFolder.Files);
            clientContext.ExecuteQuery();
            Response.Write("Root : " + ff.Name+"\r\n");
            Response.Write( " ItemCount : " + ff.ItemCount.ToString() );
            Response.Write( " Folder Count : " + ff.Folders.Count.ToString() );
            Response.Write( " File Count : " + ff.Files.Count.ToString());
            Response.Write( " URL : " + ff.ServerRelativeUrl );

            foreach (Folder f in fcol)
            {
                if (f.Name == "Testing")
                {
                    clientContext.Load(f.Files);
                    clientContext.ExecuteQuery();
                    FileCollection fileCol = f.Files;
                    foreach (File file in fileCol)
                    {
                        lstFile.Add(file.Name);
                        Response.Write(" File Name : " + file.Name);
                    }
                }


            }
        }
  • I used your code in a console application, and it worked for me. Are you sure you have folders and files in said library? – wjervis Oct 15 '14 at 12:27
  • 1
    Your camlQuery variable is declared and then set, but you never do anything with it (ie 'ListItemCollection listItems = spList.GetItems(camlQuery);') – Graham Apr 10 '15 at 21:17
1

Have you tried using SP.CamlQuery.createAllItemsQuery() instead of using your Caml Query there?

  • It worked for me, I forgot to add connection in APPManifest file. Thank you for your reply. – PR_Surya Oct 16 '14 at 6:01
  • Can you please tell me how to read all the subfolders and nested folders and files from SharePoint document library. – PR_Surya Oct 16 '14 at 12:06
  • 1
    You need a recursive method. It returns void and takes in a Folder parameter. You do your work on the parameter being passed in. You then need to load and ExecuteQuery on the Folders property. You then do a foreach over the Folders property, and within that you call the method that you're in, passing in the current iterator. To see example code of this, see here: pastebin.com/Vac3zyF8 – James Love Oct 16 '14 at 13:50
  • With same reference, how to get file names from Zip folder without extracting it. I have files, folder and zip folder in the document library. I am unable to get the file names from zip folder. – PR_Surya Oct 22 '14 at 12:07
  • A zip folder isn't a folder, it's a compressed file. You'll need to use server side code with some third party libraries to extract and inspect the file. – James Love Oct 22 '14 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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