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
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;
context.Load(folders);
context.ExecuteQuery();
foreach(Folder folder in folders)
{
Console.WriteLine(folder.Name);
}
-
-
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_MSFTCommented May 9, 2019 at 9:55