Imports System.Reflection
Imports System.Net
Imports System.Xml
Imports System.IO
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration


Public Class SPTreeView
    'Dim ds As New DataSet
    'Dim sTextToRemove As String = "/"
    'Dim alCheckedExFolders As New ArrayList
    'Dim alCheckedExMailFolders As New ArrayList
    'Dim wfLists As New Lists.Lists
    'Dim psProgress As ProgressStatus

    Private Sub btnSPBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSPBrowse.Click
        'LoadTreeView()
        Dim oSite As SPSite = New SPSite(txtSPURL.Text)
        Dim oSPWebRoot As SPWeb = oSite.OpenWeb
        treeSP.ImageIndex = 0
        treeSP.SelectedImageIndex = 0
        treeSP.Nodes.Add(oSPWebRoot.Title)
        FillSubWeb(treeSP.Nodes(0), oSite.OpenWeb)

    End Sub
    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)
            subTN.ImageIndex = 1
            subTN.SelectedImageIndex = 1
            tn.Nodes.Add(subTN)
            'add code here to iterate on LISTS of theweb object. 

            FillList(subTN, theweb)

            FillSubWeb(subTN, theweb)
            theweb.Dispose()
        Next
    End Sub
    Private Sub FillList(ByVal tn As TreeNode, ByVal webSite As SPWeb)
        Dim thelist As SPList
        For Each thelist In webSite.Lists
            Dim ListTN As TreeNode = New TreeNode(thelist.Title)
            'ImageList1. Images("abc").
            Dim logourl As String = thelist.ImageUrl
            Dim arraurl() As String = logourl.Split("/")
            ListTN.SelectedImageKey = arraurl(arraurl.Length - 1)
            ListTN.ImageKey = arraurl(arraurl.Length - 1)
            'ListTN.SelectedImageIndex = 2
            'ListTN.ImageIndex = 2
            tn.Nodes.Add(ListTN)

        Next
    End Sub

    'Private Sub LoadTreeView()
    '    Dim services As SPServiceCollection = SPFarm.Local.Services
    '    Dim service As SPService
    '    For Each service In services
    '        If service.TypeName = "Windows SharePoint Services Web Application" Then
    '            Dim webService As SPWebService = CType(service, SPWebService)
    '            Dim webApplications As SPWebApplicationCollection = webService.WebApplications

    '            Dim webApplication As SPWebApplication
    '            For Each webApplication In webApplications
    '                Dim vsNode As TreeNode = New TreeNode(webApplication.Name)
    '                vsNode.Tag = webApplication
    '                treeSP.Nodes.Add(vsNode)
    '                Dim siteCol As SPSite
    '                For Each siteCol In webApplication.Sites
    '                    Dim siteColNode As TreeNode = New TreeNode(siteCol.RootWeb.Title)
    '                    siteColNode.Tag = siteCol
    '                    vsNode.Nodes.Add(siteColNode)
    '                    LoadTreeViewForSubWebs(siteCol.RootWeb, siteColNode)
    '                    siteCol.Close()
    '                Next
    '            Next
    '        End If
    '    Next
    'End Sub


    'Private Sub LoadTreeViewForSubWebs(ByVal currentWeb As SPWeb, ByVal currentNode As TreeNode)
    '    Dim web As SPWeb
    '    For Each web In currentWeb.GetSubwebsForCurrentUser()
    '        Dim webNode As TreeNode = New TreeNode(web.Title)
    '        webNode.Tag = web
    '        currentNode.Nodes.Add(webNode)
    '        If (Not (web.Webs Is Nothing)) And (web.Webs.Count > 0) Then
    '            LoadTreeViewForSubWebs(web, webNode)
    '            web.Close()
    '        End If
    '    Next
    'End Sub



End Class