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.

1 Answer 1


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 = "[email protected]";
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;
foreach(Folder folder in folders)
  • Thank you very much
    – samve
    May 9, 2019 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, 2019 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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