C# to retrieve document library files and folders from sharePoint online sub document library

Sivasankar Rebba 51 Reputation points
2023-04-06T16:40:24.26+00:00

I am unable to get files and folders from sharePoint online sub document library.

            string siteUrl = "https://abc.com/sites/abcit";
            try
            {
                using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    System.Security.SecureString passWord = new System.Security.SecureString();
                    foreach (char c in System.Configuration.ConfigurationManager.AppSettings["password"].ToString().ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials(System.Configuration.ConfigurationManager.AppSettings["username"].ToString(), passWord);
                    Web web = clientContext.Web;                                 
                    FolderCollection folderCollection =
       web.GetFolderByServerRelativeUrl("/Shared Documents/subdoc/sub doc2").Folders;
                   
                    clientContext.Load(folderCollection);                   
                    clientContext.ExecuteQuery();

                    foreach (Folder folder in folderCollection)
                    {                        
                        var item = folder.ListItemAllFields;                       
                        var created = (DateTime)item["Created"];
                        var modified = (DateTime)item["Modified"];
                    }
                }
            }
Microsoft 365 and Office Install, redeem, activate For business Windows
Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-04-07T06:23:41.1366667+00:00

    Hi @Sivasankar Rebba You can refer to following code to retrieve folders and files in subfolder

                using (var clientContext = new
               ClientContext("https://company.sharepoint.com/sites/xxx"))
                {
                    // SharePoint Online Credentials
                    var pwd = "************";
                    var passWord = new SecureString();
                    foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials("******@xxx.onmicrosoft.com", passWord);
                    // Get the SharePoint web
                    Web web = clientContext.Web;
                    clientContext.Load(web, w => w.ServerRelativeUrl);
    
    
                    clientContext.ExecuteQuery();
                    Folder targetFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/Shared%20Documents/test");
                    clientContext.Load(targetFolder.Files);
                    clientContext.Load(targetFolder.Folders);
                    clientContext.ExecuteQuery();
    
                    Console.WriteLine(targetFolder.Files.Count());
                    Console.WriteLine(targetFolder.Folders.Count());
                    Console.ReadLine();
                }
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.