First, your code will not compile since folderName
might not be initialized before accessing it...
Secondly, the code runs fine on my end.. Are you sure your folder
contains subfolders? (i.e. folder.Folders.Count > 0
is really true
?)
I used the following code - which uses yours only slightly (string folderName = null;
instead of string folderName;
) modified:
private static void Main(string[] args)
{
var url = "https://my.share.point/sites/asdf/";
using (var ctx = new ClientContext(url))
{
var folder = ctx.Site.RootWeb.Lists.GetByTitle("Blubb").RootFolder;
ctx.Load(folder);
ctx.ExecuteQuery();
var sb = new StringBuilder();
Console.WriteLine(browse(folder, ctx, ref sb));
Console.WriteLine(sb.ToString());
}
Console.WriteLine("done");
Console.ReadLine();
}
private static string browse(Folder folder,
ClientContext ctx, ref StringBuilder str)
{
string folderName = null;
str.AppendLine("browse" + folder.Name);
ctx.Load(folder.Folders);
ctx.ExecuteQuery();
if (folder.Folders.Count > 0)
for (var i = 0; i < folder.Folders.Count; i++)
{
browse(folder.Folders[i], ctx, ref str);
folderName = folder.Folders[i].Name;
}
return folderName;
}
To explain this further I "transplanted" your algorithm to ideone. Have a look at it - there's nothing wrong with the algorithm itself (depending on what you were expecting).