Visual WebPart Basic Development--treeview control reads all folders and files in the document library (recursive method reads)
1. Create a new document library named "Test Document Library" in the deployed SharePoint site and add subfolders and sub-files at all levels for testing
2. Create a new blank SharePoint project in VS2010
3. Create a Visual Web Part
Add a TreeView control in Treeviewtestusercontrol.ascx with ID "TreeView1"
Add the following implementation code in the TreeViewTestUserControl.ascx.cs source folder
usingSystem;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;//Add ReferenceusingMicrosoft.SharePoint;namespacetreeviewproject.treeviewtest{ Public Partial classTreeviewtestusercontrol:usercontrol {protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) {binddoclib (); } } /// <summary> ///Binding Document Libraries/// </summary> Private voidBinddoclib () {stringURL =SPContext.Current.Web.Url; #regionSpsecurity.runwithelevatedprivileges (Delegate() { using(SPSite site =NewSPSite (URL)) { using(SPWeb Web =site. OpenWeb ()) {foreach(SPList listinchweb. Lists) {if(list. Basetemplate! = splisttemplatetype.documentlibrary)//Locate the document library { Continue; } Else { if(list. Title = ="Test Document Library") {TreeNode node=NewTreeNode (); Node. Text=list. Title; Node. NAVIGATEURL=list. Defaultviewurl; TREEVIEW1.NODES.ADD (node); Childdocfolder (list. RootFolder, node); } } } } } }); #endregion } /// <summary> ///recursive method to read document library/// </summary> /// <param name= "folder" >parent Folder</param> /// <param name= "CurrentNode" >Current Node</param> Private voidChilddocfolder (SPFolder folder, TreeNode currentnode) {stringURL =SPContext.Current.Web.Url; if(folder. Subfolders.count = =0)// //returns if no subfolders exist, and adds the files in the folder to the TreeView { foreach(SPFile fileinchFolder. Files)//Show sub-Files{TreeNode node=NewTreeNode (); Node. Text=file. Name; Node. NAVIGATEURL= URL +"/"+file. URL; Node. IMAGEURL="_layouts/images/"+file. ICONURL; CURRENTNODE.CHILDNODES.ADD (node); } return; } Else { foreach(SPFile fileinchFolder. Files)//add files from the current folder into the TreeView{TreeNode node=NewTreeNode (); Node. Text=file. Name; Node. NAVIGATEURL= URL +"/"+file. URL; Node. IMAGEURL="_layouts/images/"+file. ICONURL; CURRENTNODE.CHILDNODES.ADD (node); } foreach(SPFolder SPFolderinchfolder. Subfolders) {if(SPFolder. Name = ="Forms")//Remove the default system folder { Continue; } TreeNode Node=NewTreeNode (); Node. Text=SPFolder. Name; Node. NAVIGATEURL= URL +"/"+SPFolder. URL; Node. IMAGEURL="_layouts/images/folder.gif"; CURRENTNODE.CHILDNODES.ADD (node); //Add the current folder to the TreeViewChilddocfolder (spfolder, node);//Show files and folders below the current folder } } } }}
Code completion, click Test Deployment WebPart to SharePoint site, insert webpart on page, test effect as follows
- Previous article SharePoint2010 Sha
Visual WebPart Basic Development--treeview control reads all folders and files in the document library (recursive method reads)