一个非常有用的webpart--文档列表添加文件夹树

代码很简单:

// test code by [email protected]
//  2008-11-19
//
//
using  System;
using  System.Collections;
using  System.Text;
using  sharepoint  =  Microsoft.SharePoint.WebControls ;
using  wss  =  Microsoft.SharePoint.WebPartPages;
using  System.Web.UI.WebControls  ;
using  System.Web.UI;
using  System.Web.UI.Design;
using  System.Runtime.InteropServices;
using  System.Web.UI.WebControls.WebParts;
using  System.Drawing;
using  System.ComponentModel;
using  Microsoft.SharePoint;
using  Microsoft.SharePoint.WebControls;

namespace  ThinkingSoft
{
    [Guid(
" D55A1423-B38E-4c2e-9303-C234D7B7453E " )]
    
public   class  TreeListViewWebPart : wss.WebPart                
    {
        
private   string  _ListUrl  =   " http://jyserver:9000/000/Forms/AllItems.aspx "  ;
        [WebBrowsable]
        [PersonalizableAttribute]
        [DisplayName(
" 列表Url " )]
        
public   string  ListUrl
        {
            
get  {  return  _ListUrl; }
            
set  { _ListUrl  =  value; }
        }
        
        
protected   override   void  CreateChildControls()
        {
            
base .CreateChildControls();
            
try
            {
                SPList list 
=  SPContext.Current.Web.GetList(_ListUrl);

                SPView view 
=  list.DefaultView;

                SPFolder f 
=  list.RootFolder;

                TreeView tree 
=   new  TreeView();
                tree.ShowLines 
=   true ;

                
string  viewUrl  =  Page.Request.RawUrl;

                
if  (viewUrl.IndexOf( " ? " !=   - 1 )
                    viewUrl 
=  viewUrl.Split( ' ? ' )[ 0 ];

                viewUrl 
+=   " ?RootFolder= " ;

                
string  currentUrl  =  Page.Request.QueryString[ " RootFolder " ];

                
this .buildSub(viewUrl, f, tree.Nodes);

                addHtml(
" <table width='100%' border='1'><tr><td width='20%' valign='top'> " );

                
this .Controls.Add(tree);

                addHtml(
" </td><td  valign='top'> " );
                
this .Controls.Add( new  LiteralControl(view.RenderAsHtml()));
                addHtml(
" </td></table> " );
            }
            
catch  (Exception ex)
            {
                addHtml(ex.Message);                
            }

        }
        
void  addHtml(  string  html )
        {
            
this .Controls.Add( new  LiteralControl(html));
        } 
        
// 创建文件夹树
         void  buildSub(  string  viewUrl , SPFolder root , TreeNodeCollection nodes )
        {
            
foreach  (SPFolder f  in  root.SubFolders)
            {
                
if  (f.Name.ToLower()  ==   " forms " continue ;

                TreeNode n 
=   new  TreeNode();
                n.Text 
=  f.Name;
                n.NavigateUrl 
=  viewUrl  +  f.Url;

                nodes.Add(n);

                buildSub( viewUrl , f , n.ChildNodes );
            }
        }       
    }
}

你可能感兴趣的:(part)