Welcome
Thursday, January 27, 2005
+TreeView Control source code
#Region "Author Info"
'This control was written by Manuel Montes. manuel.montes@cibc.com
'The dTree javascript referenced in this control is written by Geir Landro
#End Region
Imports System.ComponentModel
Imports System.Web.UI
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
<DefaultProperty("Text"), ToolboxData("<{0}:TreeViewNav runat=server></{0}:TreeViewNav>")> Public Class TreeViewNav
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Public htmlstring As String = "" 'used to build the javascript
Public Index As Integer 'unique identifier for each node
Public TempIndex As Integer 'holds current node ID
Public SiteName As String
Public CurrentLib As SPDocumentLibrary
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter
Dim CurrentWeb As SPWeb = SPControl.GetContextWeb(Context)
Dim ParentSite As SPSite = CurrentWeb.Site ' need site to work with lists and to set the following property
ParentSite.CatchAccessDeniedException = False 'do not prompt for authentication if no access
SiteName = "LibTree" & CurrentWeb.CurrentUser.ID.ToString
Dim Libraries As SPListCollection = CurrentWeb.GetListsOfType(SPBaseType.DocumentLibrary)
Libraries.ListsForCurrentUser = True
Dim Library As SPDocumentLibrary
Dim SubIndex As Integer = 0 ' Holds the level of subs
Try
Index = 0
htmlstring = "<div class=""dtree""><p><a href=""javascript: " & SiteName &amp; ".openAll();"">Open All</a> <a href=""javascript: " & SiteName &amp; ".closeAll();"">Close All</a></p>"
htmlstring += "<script type=""text/javascript"">"
htmlstring += SiteName + " = new dTree('" + SiteName + "');"
htmlstring += SiteName + ".add(0,-1,'Site Libraries');"
'users in Readers group do not have browse directory perm used in web.subfolders cast so implemented lists instead.
For Each Library In Libraries 'iterate all the site document libraries
'If Library.Permissions.DoesUserHavePermissions(SPRights.ViewPages) And Library.Permissions.DoesUserHavePermissions(SPRights.ViewListItems) Then
If Not Library.Title = "Recycled" And Not Library.IsCatalog And Not Library.BaseTemplate = SPListTemplateType.PictureLibrary Then
CurrentLib = Library
Dim NewUrl As String() = Library.DefaultViewUrl.Split("/")
Dim CurrentFolder As SPFolder = CurrentWeb.GetFolder(NewUrl.GetValue(NewUrl.Length - 3))
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & ",0,'" & CurrentFolder.Name & "','" & CurrentFolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
SubIndex = Index
If CurrentFolder.SubFolders.Count > 0 Then
ListSubFolders(CurrentFolder, SubIndex)
End If
If CurrentFolder.Files.Count > 0 Then
ListFiles(CurrentFolder, SubIndex)
End If
End If
'End If
'SubIndex = 0
Next
'htmlstring += "LibTree.config.folderlinks = ""False"";"
htmlstring += "document.write(" & SiteName &amp; ");"
htmlstring += "<"
htmlstring += "/script"
htmlstring += "></div>"
output.Write(htmlstring)
Libraries = Nothing
Library = Nothing
'CurrentWeb.Close()
Catch UnauthorizedAccessException As Exception
output.Write(UnauthorizedAccessException.Message.ToString)
End Try
End Sub
Sub ListSubFolders(ByVal ThisFolder As SPFolder, ByVal ParentIndex As Integer)
Dim Subfolder As SPFolder
'if there are subfolders, we iterate these, else we write the current folder to the tree and exit the sub
If ThisFolder.SubFolders.Count > 0 Then
For Each Subfolder In ThisFolder.SubFolders 'iterate current folder subfolders
If Subfolder.Name <> "Forms" And Subfolder.Name.Chars(0) <> "_" Then
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & ParentIndex & ",'" & Subfolder.Name &amp; "','" & Subfolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
'if this folder has subs we must enter the sub again and start again
If Subfolder.SubFolders.Count > 0 Then
TempIndex = Index
ListSubFolders(Subfolder, Index)
If Subfolder.Files.Count > 0 Then
ListFiles(Subfolder, TempIndex)
End If
Else
'Add the current Folder's files to the tree.
If Subfolder.Files.Count > 0 Then
ListFiles(Subfolder, Index)
End If
End If
End If
Next
Else
' last subfolder in current branch; add current folder and its files to the tree and exit the sub
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & TempIndex & ",'" & ThisFolder.Name &amp; "','" & ThisFolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
If ThisFolder.Files.Count > 0 Then
ListFiles(ThisFolder, Index)
End If
End If
End Sub
Sub ListFiles(ByVal Folder As SPFolder, ByVal SubInd As Integer)
Try
If CurrentLib.EnableModeration = True Then
For Each objFile As SPFile In Folder.Files 'iterate files in current folder
If objFile.Item.ModerationInformation.Status = SPModerationStatusType.Approved Then
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & SubInd &amp; ",'" & objFile.Name & "','" & objFile.ServerRelativeUrl &amp; "','" & objFile.Name.ToString &amp; "','','_layouts/1033/images/img/" & GetFileImage(objFile.Name) & "');"
End If
Next
Else
For Each objFile As SPFile In Folder.Files 'iterate files in current folder
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & SubInd &amp; ",'" & objFile.Name & "','" & objFile.ServerRelativeUrl &amp; "','" & objFile.Name.ToString &amp; "','','_layouts/1033/images/img/" & GetFileImage(objFile.Name) & "');"
Next
End If
Catch FileList As Exception
End Try
End Sub
Function GetFileImage(ByVal FileName)
' extract file extension from filename
Dim FileExt As String = FileName.Substring(FileName.Length - 3, 3)
Dim ImageFile As String
Select Case FileExt
'files are in the images\img folder. add new for new file types as needed.
Case "doc"
ImageFile = "doc16.gif"
Case "xls"
ImageFile = "xls16.gif"
Case "ppt"
ImageFile = "ppt16.gif"
Case "vsd"
ImageFile = "vsd16.gif"
Case "mpp"
ImageFile = "mpp16.gif"
Case "pdf"
ImageFile = "pdf.gif"
Case "txt"
ImageFile = "txt16.gif"
Case "xml"
ImageFile = "xml16.gif"
Case "htm"
ImageFile = "htm16.gif"
Case Else
ImageFile = "page.gif"
End Select
Return ImageFile
End Function
End Class
'This control was written by Manuel Montes. manuel.montes@cibc.com
'The dTree javascript referenced in this control is written by Geir Landro
#End Region
Imports System.ComponentModel
Imports System.Web.UI
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
<DefaultProperty("Text"), ToolboxData("<{0}:TreeViewNav runat=server></{0}:TreeViewNav>")> Public Class TreeViewNav
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Public htmlstring As String = "" 'used to build the javascript
Public Index As Integer 'unique identifier for each node
Public TempIndex As Integer 'holds current node ID
Public SiteName As String
Public CurrentLib As SPDocumentLibrary
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter
Dim CurrentWeb As SPWeb = SPControl.GetContextWeb(Context)
Dim ParentSite As SPSite = CurrentWeb.Site ' need site to work with lists and to set the following property
ParentSite.CatchAccessDeniedException = False 'do not prompt for authentication if no access
SiteName = "LibTree" & CurrentWeb.CurrentUser.ID.ToString
Dim Libraries As SPListCollection = CurrentWeb.GetListsOfType(SPBaseType.DocumentLibrary)
Libraries.ListsForCurrentUser = True
Dim Library As SPDocumentLibrary
Dim SubIndex As Integer = 0 ' Holds the level of subs
Try
Index = 0
htmlstring = "<div class=""dtree""><p><a href=""javascript: " & SiteName &amp; ".openAll();"">Open All</a> <a href=""javascript: " & SiteName &amp; ".closeAll();"">Close All</a></p>"
htmlstring += "<script type=""text/javascript"">"
htmlstring += SiteName + " = new dTree('" + SiteName + "');"
htmlstring += SiteName + ".add(0,-1,'Site Libraries');"
'users in Readers group do not have browse directory perm used in web.subfolders cast so implemented lists instead.
For Each Library In Libraries 'iterate all the site document libraries
'If Library.Permissions.DoesUserHavePermissions(SPRights.ViewPages) And Library.Permissions.DoesUserHavePermissions(SPRights.ViewListItems) Then
If Not Library.Title = "Recycled" And Not Library.IsCatalog And Not Library.BaseTemplate = SPListTemplateType.PictureLibrary Then
CurrentLib = Library
Dim NewUrl As String() = Library.DefaultViewUrl.Split("/")
Dim CurrentFolder As SPFolder = CurrentWeb.GetFolder(NewUrl.GetValue(NewUrl.Length - 3))
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & ",0,'" & CurrentFolder.Name & "','" & CurrentFolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
SubIndex = Index
If CurrentFolder.SubFolders.Count > 0 Then
ListSubFolders(CurrentFolder, SubIndex)
End If
If CurrentFolder.Files.Count > 0 Then
ListFiles(CurrentFolder, SubIndex)
End If
End If
'End If
'SubIndex = 0
Next
'htmlstring += "LibTree.config.folderlinks = ""False"";"
htmlstring += "document.write(" & SiteName &amp; ");"
htmlstring += "<"
htmlstring += "/script"
htmlstring += "></div>"
output.Write(htmlstring)
Libraries = Nothing
Library = Nothing
'CurrentWeb.Close()
Catch UnauthorizedAccessException As Exception
output.Write(UnauthorizedAccessException.Message.ToString)
End Try
End Sub
Sub ListSubFolders(ByVal ThisFolder As SPFolder, ByVal ParentIndex As Integer)
Dim Subfolder As SPFolder
'if there are subfolders, we iterate these, else we write the current folder to the tree and exit the sub
If ThisFolder.SubFolders.Count > 0 Then
For Each Subfolder In ThisFolder.SubFolders 'iterate current folder subfolders
If Subfolder.Name <> "Forms" And Subfolder.Name.Chars(0) <> "_" Then
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & ParentIndex & ",'" & Subfolder.Name &amp; "','" & Subfolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
'if this folder has subs we must enter the sub again and start again
If Subfolder.SubFolders.Count > 0 Then
TempIndex = Index
ListSubFolders(Subfolder, Index)
If Subfolder.Files.Count > 0 Then
ListFiles(Subfolder, TempIndex)
End If
Else
'Add the current Folder's files to the tree.
If Subfolder.Files.Count > 0 Then
ListFiles(Subfolder, Index)
End If
End If
End If
Next
Else
' last subfolder in current branch; add current folder and its files to the tree and exit the sub
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & TempIndex & ",'" & ThisFolder.Name &amp; "','" & ThisFolder.ServerRelativeUrl &amp; "','','','_layouts/1033/images/img/folder.gif');"
If ThisFolder.Files.Count > 0 Then
ListFiles(ThisFolder, Index)
End If
End If
End Sub
Sub ListFiles(ByVal Folder As SPFolder, ByVal SubInd As Integer)
Try
If CurrentLib.EnableModeration = True Then
For Each objFile As SPFile In Folder.Files 'iterate files in current folder
If objFile.Item.ModerationInformation.Status = SPModerationStatusType.Approved Then
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & SubInd &amp; ",'" & objFile.Name & "','" & objFile.ServerRelativeUrl &amp; "','" & objFile.Name.ToString &amp; "','','_layouts/1033/images/img/" & GetFileImage(objFile.Name) & "');"
End If
Next
Else
For Each objFile As SPFile In Folder.Files 'iterate files in current folder
Index = Index + 1
htmlstring += SiteName & ".add(" & Index & "," & SubInd &amp; ",'" & objFile.Name & "','" & objFile.ServerRelativeUrl &amp; "','" & objFile.Name.ToString &amp; "','','_layouts/1033/images/img/" & GetFileImage(objFile.Name) & "');"
Next
End If
Catch FileList As Exception
End Try
End Sub
Function GetFileImage(ByVal FileName)
' extract file extension from filename
Dim FileExt As String = FileName.Substring(FileName.Length - 3, 3)
Dim ImageFile As String
Select Case FileExt
'files are in the images\img folder. add new for new file types as needed.
Case "doc"
ImageFile = "doc16.gif"
Case "xls"
ImageFile = "xls16.gif"
Case "ppt"
ImageFile = "ppt16.gif"
Case "vsd"
ImageFile = "vsd16.gif"
Case "mpp"
ImageFile = "mpp16.gif"
Case "pdf"
ImageFile = "pdf.gif"
Case "txt"
ImageFile = "txt16.gif"
Case "xml"
ImageFile = "xml16.gif"
Case "htm"
ImageFile = "htm16.gif"
Case Else
ImageFile = "page.gif"
End Select
Return ImageFile
End Function
End Class