Visual WebPart Basic Development--treeview control reads all folders and files in the document library (recursive method reads)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.