read subfolders in specifed folder

Occasional Visitor

Hello. I need read subfolders in specifed folder.

List<string> levels = catalog.Split('\\').ToList(); // catalog is input parameter,
// for example: catalog catalog = "DocsLibrary\\Clients\\";

levels.RemoveAt(levels.Count - 1); //levels Count = 2 [0]: "DocsLibrary" [1]: "Clients"

I split catalog and try to understand, if each folderName = levels[i] exists in rootFolder. If exists I, take this subfolder and try to get subfolder.folders


List list = contextSP.Web.Lists.GetByTitle("Documents");

var currentfolder = list.RootFolder;
contextSP.Load(currentfolder, f => f.Folders, f => f.ServerRelativeUrl);
contextSP.ExecuteQuery();

var ourfolders = currentfolder.Folders.ToList(); // ourfolders[0].Name "DocsLibrary"
string currentfolderurl = null;

for (int i = 0; i < levels.Count; i++)
{
string folderName = levels[i];

if (!ourfolders.Where(x => x.Name == folderName).Any())
{
return dict;
}
else
{
//Retrieve current folder's folders
currentfolder = ourfolders.Where(x => x.Name == folderName).First();
contextSP.Load(currentfolder, f => f.Folders, f => f.ServerRelativeUrl);
contextSP.ExecuteQuery();

}
currentfolderurl = currentfolder.ServerRelativeUrl;
ourfolders = currentfolder.Folders.ToList();
}

for i=0 I receive 13 subfolders(its right number), in next iteration I must read approximately 66.000 records, but ourfolders.Count = 0

Why ?

 

0 Replies