Displaying an automatically generated Site Map. : Site Maps « Development « ASP.NET Tutorial






File: App_Code\SqlSiteMapProvider.cs

using System;
using System.Collections.Specialized;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Caching;

namespace MyNamespace
{
    public class SqlSiteMapProvider : StaticSiteMapProvider
    {
        private bool _isInitialized = false;
        private string _connectionString;
        private SiteMapNode _rootNode;

        public override void Initialize(string name, NameValueCollection attributes)
        {
            if (_isInitialized)                return;

            base.Initialize(name, attributes);

            string connectionStringName = attributes["connectionStringName"];
            if (String.IsNullOrEmpty(connectionStringName))
                throw new Exception("You must provide a connectionStringName attribute");

            _connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            if (String.IsNullOrEmpty(_connectionString))
                throw new Exception("Could not find connection string " + connectionStringName);

            _isInitialized = true;
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return BuildSiteMap();
        }

        public override SiteMapNode BuildSiteMap()
        {
            lock (this)
            {
                HttpContext context = HttpContext.Current;
                _rootNode = (SiteMapNode)context.Cache["RootNode"];

                if (_rootNode == null)
                {
                    HttpContext.Current.Trace.Warn("Loading from database");

                    Clear();

                    DataTable tblSiteMap = GetSiteMapFromDB();

                    _rootNode = GetRootNode(tblSiteMap);                    
                    AddNode(_rootNode);

                    BuildSiteMapRecurse(tblSiteMap, _rootNode);

                    SqlCacheDependency sqlDepend = new SqlCacheDependency("SiteMapDB", "SiteMap");
                    context.Cache.Insert("RootNode", _rootNode, sqlDepend);
                }
                return _rootNode;

            }
        }


        private DataTable GetSiteMapFromDB()
        {
            string selectCommand = "SELECT Id,ParentId,Url,Title,Description FROM SiteMap";
            SqlDataAdapter dad = new SqlDataAdapter(selectCommand, _connectionString);
            DataTable tblSiteMap = new DataTable();
            dad.Fill(tblSiteMap);
            return tblSiteMap;
        }


        private SiteMapNode GetRootNode(DataTable siteMapTable)
        {
            DataRow[] results = siteMapTable.Select("ParentId IS NULL");
            if (results.Length == 0)
                throw new Exception("No root node in database");
            DataRow rootRow = results[0];
            return new SiteMapNode(this, rootRow["Id"].ToString(), rootRow["url"].ToString(), rootRow["title"].ToString(), rootRow["description"].ToString());
        }

        private void BuildSiteMapRecurse(DataTable siteMapTable, SiteMapNode parentNode)
        {
            DataRow[] results = siteMapTable.Select("ParentId=" + parentNode.Key);
            foreach (DataRow row in results)
            {
                SiteMapNode node = new SiteMapNode(this, row["Id"].ToString(), row["url"].ToString(), row["title"].ToString(), row["description"].ToString());
                AddNode(node, parentNode);
                BuildSiteMapRecurse(siteMapTable, node);
            }
        }

    }
}

            
File: Web.Config

<configuration>
  <connectionStrings>
    <add
      name="conSiteMap"
      connectionString="Data Source=.\SQLExpress;Integrated
Security=True;AttachDbFileName=|DataDirectory|SiteMapDB.mdf;User Instance=True"/>
</connectionStrings>

  <system.web>
    <siteMap defaultProvider="myProvider">
      <providers>
        <add
          name="myProvider"
          type="MyNamespace.SqlSiteMapProvider"
          connectionStringName="conSiteMap" />

      </providers>
    </siteMap>

      <caching>
      <sqlCacheDependency enabled = "true" pollTime = "5000" >
        <databases>
          <add name="SiteMapDB"
               connectionStringName="conSiteMap"
          />
        </databases>
      </sqlCacheDependency>
      </caching>

      </system.web>
</configuration>








9.40.Site Maps
9.40.1.Defining a Site Map
9.40.2.The SiteMapPath control enables you to navigate to any parent page of the current page.
9.40.3.Format SiteMapPath
9.40.4.Using a template with the SiteMapPath control.
9.40.5.Using the SiteMap Class
9.40.6.Adding nodes to a Site Map dynamically.
9.40.7.Using the SiteMapNode Class
9.40.8.To display different links to different users depending on their roles, enable the Security Trimming
9.40.9.Merging Multiple Site Maps
9.40.10.Creating Custom Site Map Attributes
9.40.11.Creating the AutoSiteMapProvider
9.40.12.Displaying an automatically generated Site Map.
9.40.13.Removing the root node from the retrieved node collection
9.40.14.using the StartFromCurrentNode property
9.40.15.Using the StartingNodeUrl property
9.40.16.Working with the CurrentNode object (C#)
9.40.17.Working with the CurrentNode object (VB)
9.40.18.Creating a custom navigation display using the CurrentNode property (C#)
9.40.19.Creating a custom navigation display using the CurrentNode property (VB)