0

I have this method :

private static string browse(Microsoft.SharePoint.Client.Folder folder, 
 ClientContext ctx,ref StringBuilder str)
        {
            string folderName;
                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;

    }

When I call it, the folderName that it returns is an empty string and I don't understand why.

5

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

7
  • Yes, there are subfolders in the SharePoint folder. I tried calling the method like this: string fldname=browse(someFolder,ctx,ref str); I wanted to print the result in console, but I don't get anything. – angie Jul 11 '17 at 12:33
  • @angie: Well, your code is working fine. The Problem is somewhere else... – Nils Jul 11 '17 at 12:50
  • Can you try printing out in console the value of folderName that is returned when the method is called? – angie Jul 11 '17 at 12:58
  • Well, it would only print output only for a folder with subfolders and in that case the name of the last subfolder. For folders without subfolders it will be empty. – Nils Jul 11 '17 at 13:32
  • It is empty even though my folder has subfolders in it and when I tried to get the length of it, I got a System.nullReference exception. – angie Jul 11 '17 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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