SharePoint - get folders tree structre from document library using the client object model

Asked By Suchithra j on 21-Aug-12 11:13 AM
    using (ClientContext context = new ClientContext(System.Configuration.ConfigurationManager.AppSettings["SiteUrl"]))
    {
      NetworkCredential credentials = new NetworkCredential(Admin, AdminPWD, AdminDomain);
      context.Credentials = credentials;
 
      List list = context.Web.Lists.GetByTitle(System.Configuration.ConfigurationManager.AppSettings["DocumentLibrary"]);
               
      context.Load(list);
      context.ExecuteQuery();
      context.Load(list.RootFolder);
      context.Load(list.RootFolder.Folders);
      context.ExecuteQuery();
 
      testnode = new System.Web.UI.WebControls.TreeNode();
       
      foreach (Folder SubFolder in list.RootFolder.Folders)
      {
        if (SubFolder.Name != "Forms")
        {
          System.Web.UI.WebControls.TreeNode MainNode = new System.Web.UI.WebControls.TreeNode(SubFolder.Name);
          GetFolderNode(MainNode, SubFolder, context);
          // testnode.ChildNodes.Add(FillTreeViewNodes(SubFolder, MainNode, context));
        
        }
       }           
    }
 
 
public List<FolderInfo> GetFoldersInFolder(Folder SubFolder, ClientContext clientcontext)
  {
    List<FolderInfo> result = new List<FolderInfo>();
    FolderInfo folderinfo;
    clientcontext.Load(SubFolder.Folders);
    clientcontext.ExecuteQuery();
   
    foreach (Folder Fol in SubFolder.Folders)
    {
      folderinfo = new FolderInfo();
         folderinfo.foldername = Fol.Name;
        if (folderinfo.foldername != "Forms")
        {
          folderinfo.folderurl = Fol.ServerRelativeUrl;
          result.Add(folderinfo);
        }
       
    }
 
    return result;
  }
 
public System.Web.UI.WebControls.TreeNode GetFolderNode(System.Web.UI.WebControls.TreeNode node, Folder folder, ClientContext clientcontext)
  {
    List<FolderInfo> folders = GetFoldersInFolder(folder, clientcontext);
      clientcontext.Load(folder.Folders);
      clientcontext.ExecuteQuery();
      System.Web.UI.WebControls.TreeNode folderNode;
      for (int documentcount = 0; documentcount <= folders.Count - 1; documentcount++)
      {
        folderNode = new System.Web.UI.WebControls.TreeNode(folders[documentcount].foldername);
        Folder testfolder = folder.Folders[documentcount];
        node.ChildNodes.Add(folderNode);
        folderNode.ChildNodes.Add(GetFolderNode(folderNode, testfolder, clientcontext));
      }
     
    return node;
 
 
  }



issue is with the Get folder node it will retrive all folder nodes once it come to returen node again the control wil go to
 

folderNode.ChildNodes.Add(GetFolderNode(folderNode, testfolder, clientcontext));
pls help me in fixing this issues.
i need solution in client object model

Robbe Morris replied to Suchithra j on 21-Aug-12 04:38 PM
You are not using recursion correctly.  You are attempting to add nodes twice to the child folder:


folderNode.ChildNodes.Add(GetFolderNode(folderNode, testfolder, clientcontext));


This is wrong...

You want to add the children to the passed in node first, then iterate through them through calling the same method recursively with the child node.  You are kind of doing that here but you are doing a .ChildNodes.Add( on the result of GetFolderNode instead of simply passing in folderNode and "its" children as the first part of the method.
Suchithra j replied to Robbe Morris on 22-Aug-12 12:37 AM
can you share a piece of code it will he help full.i mnot knwing how to add parent npde and do recursive