in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Andrew Noon's Blog

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

March 2008 - Posts

  • 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;
            }

Need SharePoint Training? Attend a SharePoint Bootcamp!

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