This article contains the source code for the Sharepoint Subsites webpart that can be downloaded here.
I will try to give a very short description for each section of code. The project is written in Visual Basic and contains comments in the code so hopefully it’s understandable.
The first part of the webpart code set a few properties that will be used in the webpart code later.
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
These are the properties that are displayed when you configure the webpart in the browser.
The code then checks the ShowDropDown property and if this is set to TRUE we will show the list of subsites as a dropdown list.
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
If ShowDropDown is set to FALSE the subsites should be displayed as a list and then this section will create a Table object and add title and url from each subsite the user has access to.
'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
The last piece of code is the SelectedIndexChanged method of the dropdown box we created in the first section. this is executed when the user selects a new item in the dropdown box and the user is redirected to the selected site. this is only used if the webpart is configured to show the subsites as a dropdown box.
'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 dropdown list 'It uses the Response.Redirect method of the page to navigate to the URL of the selected 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 always 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
The source code for the Subsites webpart can be downloaded in a zip file here. It’s the complete Visual Studio 2010 solution for the webpart.
5 thoughts on “SubSites Webpart Source Code”
I was wondering if this webpart could be used with sharepoint online. I really need this functionality.
Yes you can download a version for Office365 here http://sharepoint247.com/wp-content/plugins/download-monitor/download.php?id=7
Any way to exclude apps in the list on Office365
Haven’t looked into that yet. I’m using it on Office365 and it only shows subsites, not app sites as they are in a separate site collection.
I’m toying with making the webpart into an SharePoint hosted App but have not had much time to do so yet. Have seen that the JSOM GetSubWebsForCurrentUser returns the App web as well but have not looked into this yet.