0

We have a site which has few document libraries and each library has n number or folders and sub folders.

I want to get all the files only from the folders or sub folders which name is ABC and XYZ using CSOM or REST.

helb
  • 552
  • 3
  • 14
girish
  • 31
  • 2
  • 7
  • might be the same as https://sharepoint.stackexchange.com/questions/115980/how-to-get-all-the-files-inside-the-folder-in-list – Celophysis Jun 05 '18 at 12:49

2 Answers2

0

Below is the CSOM code to get the list of files in a folder in SharePoint online library.

static void Main(string[] args)
{
    string userName = "<user name>";
    string password = "<password>";
    var securePassword = new SecureString();
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }
    using (ClientContext cxt = new ClientContext("<your site URL>"))
    {
        cxt.Credentials = new SharePointOnlineCredentials(userName, securePassword);
        Web web = cxt.Web;
        cxt.Load(web, a => a.ServerRelativeUrl);
        cxt.ExecuteQuery();
        List list = cxt.Web.Lists.GetByTitle("<your library name >");
        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 == "<your folder name>")
            {
                cxt.Load(f.Files);
                cxt.ExecuteQuery();
                FileCollection fileCol = f.Files;
                foreach (File file in fileCol)
                {
                    lstFile.Add(file.Name);
                }
            }
        }
    }
}
helb
  • 552
  • 3
  • 14
imp. MSFT
  • 1,321
  • 7
  • 5
  • Thanks for your response. Above code will work for all the folders in the library not the subfolders. – girish Jun 07 '18 at 14:19
  • Check this article: https://sharepoint.stackexchange.com/questions/211165/get-all-document-from-all-folder-and-subfolder-from-document-library-sharepoint – imp. MSFT Jun 08 '18 at 02:00
  • I am able to get all files from all the folders and subfolders. But I need only files which has modified or created yesterday. Tried the CAML query camlQuery.ViewXml = " "; But no hope . – girish Jul 20 '18 at 12:10
0
public static void DownloadAllDocumentsfromLibrary()
{
    //ClientContext ctxSite = GetSPOContext();

ClientContext clientcontext= new ClientContext("http://your server"));
clientcontext.Load(clientcontext.Web.Lists);
clientcontext.ExecuteQuery();

   //string libraryname = "DownloadCSOM";


   foreach (List list in clientcontext.Web.Lists)
    {

    if (list.BaseType.ToString() == "DocumentLibrary")
     {

     var list = ctxSite.Web.Lists.GetByTitle(list.Title);
    var rootFolder = list.RootFolder;
    string pathString = string.Format(@"{0}{1}\", @"C:\", libraryname);
    if (!Directory.Exists(pathString))
        System.IO.Directory.CreateDirectory(pathString);
    GetFoldersAndFiles(rootFolder, ctxSite, pathString);

}




}



}

private static void GetFoldersAndFiles(Folder mainFolder, ClientContext clientContext, string pathString)
{
    try
    {
        clientContext.Load(mainFolder, k => k.Name, k => k.Files, k => k.Folders);
        clientContext.ExecuteQuery();
        foreach (var folder in mainFolder.Folders)
        {
            string subfolderPath = string.Format(@"{0}{1}\", pathString, folder.Name);
            if (!Directory.Exists(subfolderPath))
                System.IO.Directory.CreateDirectory(subfolderPath);

            GetFoldersAndFiles(folder, clientContext, subfolderPath);
        }

        foreach (var file in mainFolder.Files)
        {
            var fileName = Path.Combine(pathString, file.Name);
            if (!System.IO.File.Exists(fileName))
            {
                var fileRef = file.ServerRelativeUrl;
                var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                using (var fileStream = System.IO.File.Create(fileName))
                {
                    fileInfo.Stream.CopyTo(fileStream);
                }
            }
        }

    }
    catch (Exception ex)
    {

    }
}

private static ClientContext GetSPOContext()
{

    string UserName = "user@domain.com";
    string spsiteurl = "https://tenant.sharepoint.com/sites/sitename/";
    string Pwd = "Password";
    var secure = new SecureString();
    foreach (char c in Pwd)
    {
        secure.AppendChar(c);
    }
    ClientContext spoContext = new ClientContext(spsiteurl);
    spoContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
    return spoContext;

}
Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
kalyan
  • 1