Creating a Tree View Control WebPart

本文介绍如何使用System.Web.UI.WebControls.TreeView控件创建一个SharePoint WebPart,该WebPart能将当前网站下的所有文档库及其文件夹、文件以树状结构展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Creating a Tree View Control WebPart

I had a requirement to create a tree view control to rollup document libraries into a single view.  I chose to use the System.Web.UI.WebControls.TreeView web control.

Once you define the tree view control you add nodes, for example:

 

TreeNode childNode = new TreeNode(file.Name, "", "~/_layouts/images/" + file.IconUrl, file.ServerRelativeUrl.ToString(), "");
treeView.ChildNodes.Add(childNode);

 

 

 

I wanted to get all document libraries for a current web, I used the SPWeb.GetListsOfType method, this takes in a SPBaseType enumeration.  For example:

 

currentWeb.GetListsOfType(SPBaseType.DocumentLibrary)

 

I wrote a recursive function which loops through all the root document libraries and loops through their folders.  This looked like:

 

foreach(SPList list in currentWeb.GetListsOfType(SPBaseType.DocumentLibrary))
{
  // build the tree
  rootNode = new System.Web.UI.WebControls.TreeNode(list.Title, "", "~/_layouts/images/itdl.gif", list.RootFolder.ServerRelativeUrl.ToString(), ""); 

  // loop down the tree
  TraverseFolder(list.RootFolder, rootNode); 

  // add the root node to tree view
  treeView.Nodes.Add(rootNode); 

}

The completed webpart looks like:

 treeviewcontrol

Here's the code:

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值