今天做了一个Site Hierachy控件,可以显示当前web,以及当前web下的所有list和subweb,虽然简单,但是挺有成就感的。

在这和大家分享一下,有兴趣的童靴可以看看。代码如下。

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Site_hierachy.Layouts.Site_hierachy
{
    public partial class SiteTreeView : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Get current web
                using (SPWeb web = SPContext.Current.Web)
                {
                    //Create root node
                    TreeNode node = new TreeNode();
                    node.Text = web.Title;
                    node.Value = web.Url;

                    //If list exists, print all lists
                    if (web.Lists.Count > 0)
                    {
                        PrintLists(web, node);
                    }

                    //If subweb exists, print subweb
                    if (web.Webs.Count > 0)
                    {
                        PrintChildSite(web, node);
                    }

                    //Display root node in MyTreeView
                    MyTreeView.Nodes.Add(node);
                    MyTreeView.CollapseAll();
                }
            }
        }

        public void PrintChildSite(SPWeb web, TreeNode node)
        {
            foreach (SPWeb subWeb in web.Webs)
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = subWeb.Title;
                childNode.NavigateUrl = subWeb.Url;

                node.ChildNodes.Add(childNode);

                if (subWeb.Lists.Count > 0)
                {
                    PrintLists(subWeb,childNode);
                }

                if (web.Webs.Count > 0)
                {
                    PrintChildSite(subWeb, childNode);
                }
            }
        }

        public void PrintLists(SPWeb web, TreeNode node)
        {
            TreeNode childNode = new TreeNode();
            childNode.Text = "Lists";

            foreach (SPList list in web.Lists)
            {
                TreeNode listNode = new TreeNode();
                listNode.Text = list.Title;
                listNode.NavigateUrl = list.DefaultViewUrl;
                childNode.ChildNodes.Add(listNode);
            }
            node.ChildNodes.Add(childNode);
        }
    }
}

这是最终的结果:

Done !!!

posted on 2013-05-23 17:48  Liu_Liu  阅读(157)  评论(0编辑  收藏  举报