[SharePoint]简单实现Treeview Navigation

Treeview Navigation是一个用树形结构显示当前站点导航结构的SharePoint WebPart, 详细情况请看: http://www.cnblogs.com/rickie/archive/2004/12/18/78665.html 。
Treeview Navigation是通过作者自己写的一个MyTreeView控件来显示的,但这个控件没有开放源代码。我就用Microsoft.Web.UI.WebControls.TreeView自己写了一个简单的Treeview Navigation,代码如下:
None.gif [System.ComponentModel.Description( " AreaTreeView " )]
None.gif    
public   class  AreaTreeView : System.Web.UI.UserControl, SmartPart.IUserControl
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected System.Web.UI.WebControls.Label ErrorMessage;
InBlock.gif        
protected Microsoft.Web.UI.WebControls.TreeView AreaList;
InBlock.gif        
protected const string ImagesFolder = "wpresources/images/";
InBlock.gif        
private bool _isExpanded = true;
InBlock.gif        
private string _expandLevel;
InBlock.gif        
private Microsoft.SharePoint.SPWeb _spweb;
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
"public properties"#region "public properties"
InBlock.gif
InBlock.gif        [System.ComponentModel.Browsable(
true),
InBlock.gif        System.ComponentModel.Description(
"展开深度")]
InBlock.gif        
public string ExpandLevel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _expandLevel; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _expandLevel = value;} 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [System.ComponentModel.Browsable(
true),
InBlock.gif        System.ComponentModel.Description(
"默认展开")]
InBlock.gif        
public bool IsExpanded 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _isExpanded; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _isExpanded = value; } 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Microsoft.SharePoint.SPWeb SPWeb
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _spweb;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _spweb 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ExpandLevel != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    AreaList.ExpandLevel 
= int.Parse(ExpandLevel);//TreeView的展开深度
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                
if(AreaList.Nodes.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    AreaList.Nodes.Clear();
//清除已有节点
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                SPWeb spweb 
= null;
InBlock.gif                SPWebCollection subWebs 
= null;
InBlock.gif            
InBlock.gif                TreeNode node 
= new TreeNode();//定义根节点
InBlock.gif
                spweb = SPControl.GetContextWeb(Context);//获取当前Web站点
InBlock.gif
                subWebs = spweb.GetSubwebsForCurrentUser();//获取当前站点下的所有子站点
InBlock.gif
                if(this.SPWeb.ID == spweb.ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    node.Text 
= "<b>"+spweb.Title+"</b>";//当前站点粗体显示
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    node.Text 
= spweb.Title;
ExpandedSubBlockEnd.gif                }

InBlock.gif                node.Expanded 
= this.IsExpanded;//是否展开根节点
InBlock.gif
                node.NavigateUrl = spweb.Url;
InBlock.gif                AreaList.Nodes.Add(node);
InBlock.gif                AddTreeNode(spweb,AreaList.Nodes[
0]);//调用递归函数生成所有子节点
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ErrorMessage.Text 
= ex.Message;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void AddTreeNode(SPWeb currentWeb, TreeNode parentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
foreach(SPWeb web in currentWeb.GetSubwebsForCurrentUser())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeNode node 
= new TreeNode();
InBlock.gif                
if(this.SPWeb.ID == web.ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    node.Text 
= "<b>"+web.Title+"</b>";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    node.Text 
= web.Title;
ExpandedSubBlockEnd.gif                }

InBlock.gif                node.NavigateUrl 
= web.Url;
InBlock.gif                parentNode.Nodes.Add(node);            
InBlock.gif                AddTreeNode(web,node);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

源代码下载地址: http://files.cnblogs.com/dudu/AreaTreeView.rar
部置方法:
1、下载并安装iewebcontrols: http://files.cnblogs.com/dudu/iewebcontrols.rar
2、如果没有安装SmartPart 1.0, 请下载并安装: http://www.gotdotnet.com/workspaces/workspace.aspx?id=6cfaabc8-db4d-41c3-8a88-3f974a7d0abe
3、排除管理路径webctrl_client,该目录是在安装iewebcontrols时生成的,如果不排除的话,就无法显示TreeView(经过 kaneboy的指点, 我才知道这个的, 感谢 kaneboy),操作方法:在Windows SharePoint Services 管理中心=》配置虚拟服务器设置=》选择你的虚拟服务器=》定义管理路径中添加新路径webctrl_client(排除的路径)。
4、将DUDU.AreaTreeView.dll复制到SharePoint站点的bin目录中(如果没有该目录,就手工创建)。
5、将AreaTreeView.ascx复制到SharePoint站点的UserControls目录中(如果没有该目录,就手工创建)。

我觉得Treeview Navigation如果只显示站点与子站点的结构,并不是很实用,我们更需要显示区域的结构,这样才能全面显示站点的导航结构,使用起来才更方便,所以我想开发一个显示区域结构的AreaTreeView。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值