Skip to main content
posted the example to ideone
Source Link
Nils
  • 2.3k
  • 15
  • 32

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;
}

The list looks like this:
Folder-Structure in my list

and the outcome like this:
Outcome of the code

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).

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;
}

The list looks like this:
Folder-Structure in my list

and the outcome like this:
Outcome of the code

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;
}

The list looks like this:
Folder-Structure in my list

and the outcome like this:
Outcome of the code

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).

my code & the outcome
Source Link
Nils
  • 2.3k
  • 15
  • 32

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;
}

The list looks like this:
Folder-Structure in my list

and the outcome like this:
Outcome of the code

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?)

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;
}

The list looks like this:
Folder-Structure in my list

and the outcome like this:
Outcome of the code

Source Link
Nils
  • 2.3k
  • 15
  • 32

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?)