1

How can I get a tree view that contains all the sites and subsites, and the list of libraries for each one of those?

(I use C# , Office 365, Windows Forms)

I tried this and it's ok but i just have subsites of level 1 , I didn't find a solution to do it recursively ( I want to have all subsites in a treeview when I load my form )

    string p_path = "https://XXXX.sharepoint.com/";
    string mainpath = https://XXXX.sharepoint.com/;
    try
    {
        clientContext = new ClientContext(p_path);
        SecureString passWord = new SecureString();
        foreach (char c in ("your password")) passWord.AppendChar(c);
        clientContext.Credentials = new SharePointOnlineCredentials("your login", passWord);
        Web oWebsite = clientContext.Web;
        clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
        clientContext.ExecuteQuery();
        int i = 1;
        foreach (Web orWebsite in oWebsite.Webs)
        {

            string newpath = mainpath + orWebsite.ServerRelativeUrl;

            treeView1.Nodes.Add(orWebsite.Title);
            treeView1.Nodes[i].ForeColor = Color.Navy;
            clientContext.Load(orWebsite.Lists);
            clientContext.ExecuteQuery();

            foreach (List list in orWebsite.Lists)
            {
                if (list.BaseType == BaseType.DocumentLibrary)

                    treeView1.Nodes[i].Nodes.Add(list.Title);
            }
            i++;

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
1

Here is an example of how to make it recursive (includes only the web title portion, you'll have to add the list functionality):

ClientContext ctx = null;
try
{
    ctx = new ClientContext( siteUrl )
    {
        Credentials = _credentials
    };

    var oWeb = ctx.Web;
    ctx.Load( oWeb, web => web.Title, web => web.Webs );
    ctx.ExecuteQuery();

    treeView1.Nodes.Add( oWeb.Title );
    treeView1.Nodes[0].ForeColor = Color.Navy;

    if ( oWeb.Webs.Count >= 1 ) GetSubWebs( ctx, oWeb, 1 );

} finally { if ( ctx != null ) ctx.Dispose(); }

The recursive function (this can probably done better, and in a way where it isn't called a single time outside the function):

private void GetSubWebs( ClientContext ctx, Web web, Int32 node )
{
    ctx.Load(
            web,
            oWeb =>
                oWeb.Webs.Include(
                    subweb => subweb.Title,
                    subweb => subweb.ServerRelativeUrl,
                    subweb => subweb.Webs ) );
    ctx.ExecuteQuery();
    foreach ( var subWeb in web.Webs )
    {
        var url = _serverUrl + subWeb.ServerRelativeUrl;
        treeView1.Nodes.Add( subWeb.Title );
        treeView1.Nodes[node].ForeColor = Color.Navy;
        node++;
        if ( subWeb.Webs.Count >= 1 ) GetSubWebs( ctx, subWeb, node );
    }
}

I did this rather rushed, so like I mentioned before, it can probably be improved upon. I hope this can at least get you started.

|improve this answer|||||
  • this code don't show the libraries of each site and subsite ! – Khaw'la Karray Mar 31 '15 at 15:39

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.