Archive

Archive for November, 2008

Display SPWeb and sub SPWebs in a Tree Control

November 11, 2008 2 comments
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

oSiteTree.Nodes.Add(

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"
            tn.ChildNodes.Add(subTN)

            ‘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)
            theweb.Dispose()
        Next
End Sub

Advertisements
Categories: Sharepoint 2007