Pat’s .Net and Sharepoint Blog
In the Microsoft Sharepoint and .Net Trenches

Using the Sharepoint SPSiteMapProvider to Populate a TreeView Control

June 25th, 2008 . by <Patrick/>

Here’s a very simple way to populate a TreeView Control using the SPSiteMapProvider.

Just copy the code below, create an aspx page, name it what you want and then copy it to

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

 

Tree View 

 

Code for the application page:

<%@ Assembly Name=”Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”%>
<%@ Page language=”C#” MasterPageFile=”~/_layouts/application.master” %>
<%@ Import Namespace=”Microsoft.SharePoint.ApplicationPages” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”WebPartPages” Namespace=”Microsoft.SharePoint.WebPartPages” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”wssawc” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register TagPrefix=”wssuc” TagName=”ButtonSection” src=”~/_controltemplates/ButtonSection.ascx” mce_src=”~/_controltemplates/ButtonSection.ascx” %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Import Namespace=”Microsoft.SharePoint.Navigation” %>
<%@ Import Namespace=”System.Net” %>
<asp:Content ID=”Content1″ ContentPlaceHolderId=”PlaceHolderPageTitle” runat=”server”>
<asp:Literal id=”PageTitleLabel1″ runat=”server” ></asp:Literal>
</asp:Content>
<asp:Content ID=”Content2″ ContentPlaceHolderId=”PlaceHolderPageTitleInTitleArea” runat=”server”>
<asp:Label id=”PageTitleLabel” runat=”server” ></asp:Label>
</asp:Content>
<asp:Content ID=”Content3″ ContentPlaceHolderId=”PlaceHolderAdditionalPageHead” runat=”server”>
<script runat=”server”>
    void Page_Load(object sender, System.EventArgs e)
    {
           SPSiteMapProvider sitemapProvider = (SPSiteMapProvider)SiteMap.Providers[”SPSiteMapProvider”];
           SiteMapDataSource datasource = new SiteMapDataSource();
           datasource.Provider = sitemapProvider;
           this.TreeView1.DataSource = datasource;
           this.TreeView1.ExpandDepth = 1;
           this.TreeView1.DataBind();
    }

</script>
</asp:Content>
<asp:Content ID=”Content4″ ContentPlaceHolderId=”PlaceHolderPageImage” runat=”server”>
<img src=”/_layouts/images/blank.gif” mce_src=”/_layouts/images/blank.gif” width=1 height=1 alt=”" />
</asp:Content>
<asp:Content ID=”Content5″ ContentPlaceHolderId=”PlaceHolderPageDescription” runat=”server”>
  �
    <table cellpadding=”2″ cellspacing=”1″ width=”100%” class=”ms-informationbar” style=”margin-bottom: 5px;” border=0>
                <tr>
                    <td width=”10″ valign=”middle” style=”padding: 4px”>
                        <img id=”Img1″ src=”/_layouts/images/exclaim.gif” mce_src=”/_layouts/images/exclaim.gif” alt=”<%$Resources:wss,exclaim_icon%>” runat=”server”/>
                    </td>
                    <td class=”ms-descriptiontext”>
                        <SharePoint:EncodedLiteral ID=”EncodedLiteral1″ runat=”server” text=”Please select the site you wish to visit” EncodeMethod=’HtmlEncode’/>
                    </td>
                </tr>
              �
            </table>

    <asp:PlaceHolder id=”PublishDescription” Visible=”true” runat=”server”>
    <tr>
        <TD height=”8px”><img src=”/_layouts/images/blank.gif” mce_src=”/_layouts/images/blank.gif” width=”1″ height=”8″ alt=”"></td>
    </tr>
  �
    <tr>
      �
        <td colspan=”2″>
          �
            <table width=”100%”>
                <tr>
                    <td class=”ms-formbody”>
                        <asp:TreeView�
                            ID=”TreeView1″
                            ExpandDepth=”3″
                            Font-Names= “Arial”
                            Font-Size=”10pt”
                            ForeColor=”Blue”
                            NodeWrap=”true”
                            runat=”server”
                            >
                        </asp:treeview>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    </asp:PlaceHolder>
</asp:Content>

Â


Leave a Reply

You must be logged in to post a comment.