Home > Sharepoint 2007 > Display SPWeb and sub SPWebs in a Tree Control

Display SPWeb and sub SPWebs in a Tree Control

I wanted to create a tree view control look in the same way Sharepoint has in "Manage Content and Structure" page. I had a publishing site and my objective was to list all webs and sub webs as tree control and clicking on a tree node should pull pages from publishing web page library. I used the recursive approach to list the SPWebs in tree control. to quick develop and test this code, i created a simple web application in Visual studio, Referenced the Sharepoint dll and designed my simple page. I wrote the following code and placed my ASPX in sharepoint 12 hive Layouts folder and dll in 80/bin folder. This way i was able to run my code quick as a Layout application in Sharepoint 2007. I had 130 sites in total in following structure (5 levels)
Top Level
———–Level 1
—————–Level 1 a
—————————-Level 1 a subweb1
————————————-Level 1 a subweb1_a
—————————-Level 1 a subweb2
—————–Level 2 a
—————–Level 3 a
Displaying just SPwebs in Tree Control worked perfectly and it was displaying tree control in less than 5 seconds. i was thinking i will face performance issue but it was not the case in 130 sites. Below is the code, please note that oSiteTree is the tree control on ASPX page, please add this control and name it oSiteTree if you are just copying and pasting this code.

‘Call this one either in a button click or page load

Dim oSite as SPSite = New SPSite("http://yoursharepointURL")

Dim oSPWebRoot As SPWeb = oSite.OpenWeb


New TreeNode(" " + oSPWebRoot.Title, oSPWebRoot.ServerRelativeUrl))

FillSubWeb(oSiteTree.Nodes(0), oSite.OpenWeb)

‘Fill Sub Web call

 Private Sub FillSubWeb(ByVal tn As TreeNode, ByVal webSite As SPWeb)
        Dim theweb As SPWeb
        For Each theweb In webSite.Webs
            Dim subTN As TreeNode = New TreeNode("  " + theweb.Title, theweb.ServerRelativeUrl)
            subTN.ImageUrl = "~/_layouts/Images/cat.gif"

            ‘if you need to display doc libraries or list of SPWeb then add your code here to iterate on LISTS of theweb object

            FillSubWeb(subTN, theweb)
End Sub

Categories: Sharepoint 2007
  1. John
    March 24, 2011 at 5:55 am

    Thank you very much for the solution.
    It saved me alot of time.

  2. Moon
    February 15, 2012 at 11:15 am

    Great post thanks!!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: