Hi all,
I've downloaded an open source webpart for SharePoint. When the webpart is added to a site, it shows all the subsites underneath the home site. This works fine. The only thing it doesn't do, which I'd like it to do, is show all the child sites:
So, where it shows:
HomePage
-SubSite1
-SubSite2
-SubSite3
I want it to show:
HomePage
-SubSite1
--ChildSite1
-SubSite2
--ChildSite1
--ChildSite2
-SubSite3
--ChildSite1
etc,
I've attached the code part I think is relevant, just hoping someone can help me add this iterative approach.
Thanks
I've downloaded an open source webpart for SharePoint. When the webpart is added to a site, it shows all the subsites underneath the home site. This works fine. The only thing it doesn't do, which I'd like it to do, is show all the child sites:
So, where it shows:
HomePage
-SubSite1
-SubSite2
-SubSite3
I want it to show:
HomePage
-SubSite1
--ChildSite1
-SubSite2
--ChildSite1
--ChildSite2
-SubSite3
--ChildSite1
etc,
I've attached the code part I think is relevant, just hoping someone can help me add this iterative approach.
Thanks
Code:
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
<ToolboxItemAttribute(False)> _
Public Class SubWebs
Inherits WebPart
Protected WithEvents SubWebList As New DropDownList
'these constants holds the default values for the webpart properties
Private Const _defaultText As String = ""
Private Const _defaultImage As Boolean = True
Private Const _defaultBool As Boolean = True
Private Const _defaultDesc As Boolean = False
Private Const _defaultdropdown As Boolean = False
Private Const _defaultIcon As Boolean = False
Dim _text As String = _defaultText
Dim _bolImage As Boolean = _defaultImage
Dim _bool As Boolean = _defaultBool
Dim _Desc As Boolean = _defaultDesc
Dim _dropdown As Boolean = _defaultdropdown
Dim _Icon As Boolean = _defaultIcon
'Webpart property to show small square image in front of listing
<Personalizable(PersonalizationScope.[Shared]), _
WebBrowsable(True), DefaultValue(_defaultImage), WebDisplayName("Show bullet in list"), WebDescription("Show bullet in list")> _
Property ShowImage() As Boolean
Get
Return _bolImage
End Get
Set(ByVal Value As Boolean)
_bolImage = Value
End Set
End Property
'webpart property, true will open link in new browser window
<Personalizable(PersonalizationScope.[Shared]), _
WebBrowsable(True), DefaultValue(_defaultBool), WebDisplayName("Open link in new Window"), WebDescription("Open link in new Window")> _
Property NewWindow() As Boolean
Get
Return _bool
End Get
Set(ByVal Value As Boolean)
_bool = Value
End Set
End Property
'Webpart property, set to true to show site description togheter with the sitename
<Personalizable(PersonalizationScope.[Shared]), _
WebBrowsable(True), DefaultValue(_defaultDesc), WebDisplayName("Show site description"), WebDescription("Show site description")> _
Property ShowDesc() As Boolean
Get
Return _Desc
End Get
Set(ByVal Value As Boolean)
_Desc = Value
End Set
End Property
'webpart property, set to true to show an icon representing the type of site, will show differnet builtin icons for team site, meeting workspace etc
<Personalizable(PersonalizationScope.[Shared]), _
WebBrowsable(True), DefaultValue(_defaultIcon), WebDisplayName("Show icon"), WebDescription("Show icon for type of site")> _
Property ShowIcon() As Boolean
Get
Return _Icon
End Get
Set(ByVal Value As Boolean)
_Icon = Value
End Set
End Property
'webpart property, if set to true the subsites will be dispalyed in a dropdown list instead of a list, use this to save space if you have a lot of subsites
<Personalizable(PersonalizationScope.[Shared]), _
WebBrowsable(True), DefaultValue(_defaultdropdown), WebDisplayName("Show as Dropdown list"), WebDescription("Show sites in dropdown list")> _
Property ShowDropDown() As Boolean
Get
Return _dropdown
End Get
Set(ByVal Value As Boolean)
_dropdown = Value
End Set
End Property
Protected Overrides Sub CreateChildcontrols()
'if webpart property is set to show as dropdown list use this portion of code
If ShowDropDown = True Then
SubWebList.AutoPostBack = True
'Add dummy item at top of dropdown list, this adds the text "Select Site or Workspace" as the top item in the dropdown box.
SubWebList.Items.Add(New ListItem("Select Site or Workspace", "NOSITE"))
Try
'Get SPWeb object representing the current site
Dim myWeb As SPWeb = SPControl.GetContextWeb(Context)
'Get collection of subsites the user has permissions to see
Dim subSites As SPWebCollection = myWeb.GetSubwebsForCurrentUser
'loop through the subsites collection
Dim intY As Integer
For intY = 0 To subSites.Count - 1
'add subsite title and url to the dropdown list
SubWebList.Items.Add(New ListItem(subSites(intY).Title, subSites(intY).Url))
Next
'Center the dropdown list in the webpart.
'This should probably be removed and styled with CSS instead.
Dim wssLit As New Literal
wssLit.Text = " <div style='text-align:center'>"
'add the div tag to center the dropdown list to the Controls collection of the webpart
Controls.Add(wssLit)
'add the dropdown list to the webparts controls collection
Controls.Add(SubWebList)
'add antoher Literal control to the controls collection, this is to close the Div tag
Dim wssCloseLit As New Literal
wssCloseLit.Text = "</Div>"
Controls.Add(wssCloseLit)
'catch any errors
Catch
Dim lblError As New Label
lblError.Text = "An unknow error occured."
Controls.Add(lblError)
End Try
'This Else clause is used if Show subsites as list is selected E.g ShowDropDown = False
Else
'create a new table
Dim table1 As New Table
table1.CellPadding = 2
Try
'Get SPWeb object representing the current site
Dim myWeb As SPWeb = SPControl.GetContextWeb(Context)
'Get collection of subsites the user has permissions to see
Dim subSites As SPWebCollection = myWeb.GetSubwebsForCurrentUser
Dim intY As Integer
'Loop through the subsites collection and add a table row to table1 for each subsite
For intY = 0 To subSites.Count - 1
Dim kisRow As New TableRow
Dim strLink As String
Dim kisCell1 As New TableCell
'If Open link in new window is set add the target=_new attribute to the <a href tag
If _bool Then
strLink = "<a href='" & subSites(intY).Url & "' target=_nsp>" & subSites(intY).Title & "</a>"
'Else just add the link
Else
strLink = "<a href='" & subSites(intY).Url & "'>" & subSites(intY).Title & "</a>"
End If
'Check if we should show icon
If ShowIcon Then
'If the site is a Meeting Workspace add the Meeting workspace icon
If subSites(intY).WebTemplate.ToUpper = "MPS" Then
strLink = "<img src=/_layouts/images/mtgicon.gif alt='Meeting Workspace'> " & strLink
'If the subsite is a Docuemnt Workspace add the document workspace icon
ElseIf subSites(intY).WebTemplate.ToUpper = "STS" AndAlso subSites(intY).Configuration = 2 Then
strLink = "<img src=/_layouts/images/docicon.gif ALT='Document Workspace'> " & strLink
'for all other sites add the standard Team Site icon
Else
strLink = "<img src=/_layouts/images/stsicon.gif ALT='Team Site'> " & strLink
End If
End If
'check if we should show a image bullet
If _bolImage Then
Dim kisCell As New TableCell
'this is a standard Sharepoint gif
kisCell.Text = "<img src=/_layouts/images/square.gif>"
kisRow.Cells.Add(kisCell)
End If
'add the link text to the table cell
kisCell1.Text = strLink
kisRow.Cells.Add(kisCell1)
table1.Rows.Add(kisRow)
'if Show Description is set to true we will add a cell to show the Site Descrition as well. this description is the description you put on the site when you created the subsites
If _Desc Then
Dim nspRow As New TableRow
Dim nspCell As New TableCell
Dim nspCell1 As New TableCell
nspCell1.Text = ""
nspCell.Text = subSites(intY).Description
nspRow.Cells.Add(nspCell1)
nspRow.Cells.Add(nspCell)
table1.Rows.Add(nspRow)
End If
Next
'that's it, add the table to the webparts control collection and your done
Controls.Add(table1)
'catch any errors
Catch
Dim lblError As New Label
lblError.Text = "Error getting subsites"
Controls.Add(lblError)
End Try
End If
'Execute the CreateChildControls collection to render the webpart
MyBase.CreateChildControls()
End Sub
'this is the SelectedIndexChanged method of the Dropdown list
'It is executed if you selct to show the subsites list as a dropdown box. It is executed if you select an item in the drpdown list
'It uses the Response.Redirect method of the page to naviagte to the URL of the selcted subsite.
'NOTE: If you choose to display the list as a dropdown list the Subsites will not be opened in a new browser window, it will allways open in the same browser window.
Private Sub SubWebList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubWebList.SelectedIndexChanged
If Not SubWebList.SelectedValue = "NOSITE" Then
Me.Page.Response.Redirect(SubWebList.SelectedValue)
End If
End Sub
End Class