sharepoint 2013 获取文档库文件夹树形结构
分类: sharepoint2013-07-15 06:40 5816人阅读 评论(0) 收藏 举报
在做sharepoint 2013或者sharepoint 2010文档库的过程中,经常会建很多的文件夹,不同类别存储不同文件,相当于文档的分类,这里主要是记录一下,将文档库的文件夹结构,绑定到treeview。
protected void Page_Load(object sender,EventArgse)
{
if(!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
SPSite site =newSPSite("http://win2012sp/");
SPWeb spWeb = site.OpenWeb();
SPList spList = spWeb.Lists["共享文档"];
tvTreeCatalog.Nodes.Add(CreateNodeByList(spList));
spWeb.Dispose();
}
private TreeNode CreateNodeByList(SPList spList)
{
TreeNode node =newTreeNode();
node.Text = spList.Title;
node.Value = spList.RootFolder.Url;
foreach(SPFolder subFolder in spList.RootFolder.SubFolders)
{
if(subFolder.Name !="Forms")
{
node.ChildNodes.Add(CreateNodeByFolder(subFolder));
}
}
return node;
}
private TreeNode CreateNodeByFolder(SPFolderspFolder)
{
TreeNode node =newTreeNode();
node.Text = spFolder.Name;
node.NavigateUrl = SPContext.Current.Web.Lists["共享文档1"].DefaultViewUrl+"?RootFolder="+spFolder.Url;
node.ImageUrl = SPControl.GetContextWeb(Context).Url+"/_layouts/images/folder.gif";
foreach(SPFolder subFolder in spFolder.SubFolders)
{
node.ChildNodes.Add(CreateNodeByFolder(subFolder));
}
return node;
}