in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Andrew Noon's Blog

SharePoint, MOSS 2007, Information Architecture, Business Intelligence and other rambling

Everyone wants one!

I've written so many SharePoint asset viewer type web parts (or applications) over the last few years I thought it was about time I blogged some code to get you started yourselves, the following will render a simple treeview that will allow you to traverse through your SharePoint site and sub sites and see what lists, libraries, users etc are in each site.

  image
        /// <summary>
        /// PopulateTreeView is a recursive function, starting at the supplied SPWeb parameter it will extract all relevant information from the site before recirsing for each sub site.
        /// </summary>
        /// <param name="web">web is the starting SPWeb (SharePoint web site) for the function.  In this case the current site i.e. the site on which the web part is placed.</param>
        /// <returns></returns>
        protected TreeNode PopulateTreeView(SPWeb web)
        {
            TreeNode top = null;
            if (web != null)
            {
                //get detail of this current web
                top = new TreeNode("Site Details(" + web.Title + ")", "0", "/_layouts/images/SPHOMESM.GIF", web.Url, "_blank");
                TreeNode node = new TreeNode(web.Title, "root", "/_layouts/images/ICHTT.GIF", web.Url, "_blank");
                node.ChildNodes.Add(new TreeNode("URL: " + web.Url, "", "/_layouts/images/NEWLINK.GIF", web.Url, "_blank"));
                TreeNode users = new TreeNode("Users", "", "/_layouts/images/stsicon.gif");
                //get all admins
                TreeNode admins = new TreeNode("Administrators (" + web.SiteAdministrators.Count + ")", "", "/_layouts/images/STSPEOPL.GIF");
                foreach (SPUser user in web.SiteAdministrators)
                {
                    TreeNode usr = new TreeNode(user.LoginName + "(" + user.Email + ")", "", "/_layouts/images/STAR.GIF", "mailto:" + user.Email, "");
                    admins.ChildNodes.Add(usr);
                }
                users.ChildNodes.Add(admins);
                //get all groups and users within those groups
                foreach (SPGroup group in web.Groups)
                {
                    //record this group
                    TreeNode grp = new TreeNode(group.Name + " (" + group.Users.Count + ")", "", "/_layouts/images/STSPEOPL.GIF");
                    //for for each user in this group
                    foreach (SPUser user in group.Users)
                    {
                        //record this user
                        TreeNode usr = new TreeNode(user.LoginName + "(" + user.Email + ")", "", "/_layouts/images/mysite_titlegraphic.gif", "mailto:" + user.Email, "");
                        grp.ChildNodes.Add(usr);
                    }
                    users.ChildNodes.Add(grp);
                }
                node.ChildNodes.Add(users);
                //get all lists (includes document libraries, discussion forums...)
                TreeNode lists = new TreeNode("Lists and libraries (" + web.Lists.Count + ")", "", "/_layouts/images/sts_list16.gif");
                foreach (SPList list in web.Lists)
                {
                    TreeNode lst = null;
                    try
                    {
                        //an exception can be thrown if an attempt to get the amount of items in a list returns null, also if the list is partially defines an exception may be thrown
                        lst = new TreeNode(list.Title + " (" + list.ItemCount + " items)", "", "/_layouts/images/iKpiList.png", list.DefaultViewUrl, "_blank");
                    }
                    catch(Exception)
                    {
                        //if the exception (above) was thrown, just record the name of the list (ignore the amount of items)
                        lst = new TreeNode(list.Title);
                    }
                    lists.ChildNodes.Add(lst);
                }
                node.ChildNodes.Add(lists);
                //recurse for each sub web of the current web
                TreeNode webs = new TreeNode("Sub Webs (" + web.Webs.Count + ")", "", "/_layouts/images/SITEVARIATION.GIF");
                foreach (SPWeb subweb in web.Webs)
                {
                    webs.ChildNodes.Add(PopulateTreeView(subweb));
                }
                node.ChildNodes.Add(webs);
                top.ChildNodes.Add(node);
            }
            //return the complete node
            return top;
        }

Comments

 

Links (3/20/2008) « Steve Pietrek’s SharePoint Stuff said:

Pingback from  Links (3/20/2008) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

March 20, 2008 7:38 PM
 

Chris said:

The code seems to be cut off...any chance of having available as download or in some complete format?

Thanks...looks very helpful.

March 22, 2008 12:43 PM
 

Mirrored Blogs said:

Hay muchas formas, y WebParts para ver todo el contenido del sitio, pero sinceramente esta ha sido una

March 23, 2008 5:41 PM
 

Andrew Noon said:

It's not cut off - that's all there is.  

Just paste it into a webpart and make the call to PopulateTreeView!

March 24, 2008 3:48 AM
 

James said:

The code is cut off.  It's not displaying in the page correctly.  The right hand side of the code is not appearing at all.  Can you include a code download?

This code would greatly help me understand how the sharepoint sites are organised in general.

Many thanks

June 10, 2008 3:02 PM
 

james said:

sorry for the second post but I get it now.  If you drag the mouse to select the code you get it all.  It's just not being displayed properly.

June 10, 2008 3:06 PM
 

james said:

I used the code and it worked well.  However, when I added a TreeNodeExpanded event handler, nodes below a depth of 2 would not open.  I've traced this to the above function but cannot work out what's wrong.  Any suggestions?

June 14, 2008 10:07 AM
 

Frank said:

I am new in Moss Programming and have a little question.

I paste the code into a webpart, but i dont know how i should make the call? In the same class?

Thank you for your great job

July 8, 2008 6:08 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About Andrew Noon

Technical Consultant Strengths  Microsoft Office SharePoint Server 2007  SharePoint Portal Server 2003  Windows SharePoint Services  Enterprise Content Management  Business Process and Workflow Skills  Microsoft .NET C#  SQL Server  ASP.NET  K2  Information Architecture Certifications  Microsoft Certified Professional (MCP)  FileNet Certified Professional  Sun Java Certified Professional

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts