1

I want a list of folders in a document library avoiding any subfolders or files in it, using CSOM in Sharepoint Online. Could some one share the code for the same.

2

If you want to only list 1 level folders from a document library in SharePoint Online, we can use the CSOM C# code below to achieve it.

string siteUrl = "https://test.sharepoint.com/sites/lz";
var login = "lz@test.onmicrosoft.com";
var password = "xx";
var listName="Library";

ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(login, securePassword);

List list = context.Web.Lists.GetByTitle(listName);

FolderCollection folders = list.RootFolder.Folders;
context.Load(folders);
context.ExecuteQuery();
foreach(Folder folder in folders)
{
    Console.WriteLine(folder.Name);
}
  • Thank you very much – samve May 9 at 7:58
  • You can mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum. – LZ_MSFT May 9 at 9:55

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.