Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

StaticSiteMapProvider.BuildSiteMap Method

Note: This method is new in the .NET Framework version 2.0.

When overridden in a derived class, loads the site map information from persistent storage and builds it in memory.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public abstract SiteMapNode BuildSiteMap ()
public abstract SiteMapNode BuildSiteMap ()
public abstract function BuildSiteMap () : SiteMapNode

Return Value

The root SiteMapNode of the site map navigation structure.

The BuildSiteMap method is the one abstract member of the StaticSiteMapProvider class. The BuildSiteMap method is called to load and build the site map node from persistent storage. When you implement the BuildSiteMap method, ensure that it is thread-safe, because multiple concurrent page requests can result indirectly in multiple calls to load site map information.

Notes to Inheritors When overriding the BuildSiteMap method in a derived class, be sure to normalize the URLs of SiteMapNode objects that you add to your site map provider, so that the FindSiteMapNode method can retrieve a site map node regardless of whether the URL for the site map node is supplied as an absolute virtual path or application relative path. Site map provider implementers using the AddNode method should normalize any URLs before storing SiteMapNode objects in the internal hash tables on behalf of the site map provider. Security trimming behavior is included in the SiteMapProvider and StaticSiteMapProvider class implementations. However, for security trimming to function in derived classes, you must set the Roles property of any SiteMapNode objects that you create while building a site map provider when you override the BuildSiteMap method.

NoteImportant:

The BuildSiteMap method is called by the default implementation of the FindSiteMapNode, GetChildNodes, and GetParentNode methods. If you override the BuildSiteMap method in a derived class, ensure that it loads site map data only once and returns on subsequent calls.

The following code example demonstrates how to implement the BuildSiteMap method to retrieve data from a Microsoft Access database and build SiteMapNode objects that are added to the ChildNodes collection of the root site map node. Finally, the RootNode property is returned to the caller.

This code example is part of a larger example provided for the StaticSiteMapProvider class.

// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
public override SiteMapNode BuildSiteMap() {

    // Since the SiteMap class is static, make sure that it is
    // not modified while the site map is built.
    lock(this) {

        // If there is no initialization, this method is being
        // called out of order.
        if (! IsInitialized) {
            throw new Exception("BuildSiteMap called incorrectly.");
        }

        // If there is no root node, then there is no site map.
        if (null == rootNode) {
            // Start with a clean slate
            Clear();

            // Select the root node of the site map from Microsoft Access.
            int rootNodeId = -1;

            if (accessConnection.State == ConnectionState.Closed)
                accessConnection.Open();
            OleDbCommand rootNodeCommand =
                new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
                                 accessConnection);
            OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();

            if(rootNodeReader.HasRows) {
                rootNodeReader.Read();
                rootNodeId = rootNodeReader.GetInt32(0);
                // Create a SiteMapNode that references the current StaticSiteMapProvider.
                rootNode   = new SiteMapNode(this,
                                             rootNodeId.ToString(),
                                             rootNodeReader.GetString(1),
                                             rootNodeReader.GetString(2));

            }
            else return null;

            rootNodeReader.Close();
            // Select the child nodes of the root node.
            OleDbCommand childNodesCommand =
                new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?",
                                 accessConnection);
            OleDbParameter rootParam = new OleDbParameter("parentid", OleDbType.Integer);
            rootParam.Value = rootNodeId;
            childNodesCommand.Parameters.Add(rootParam);

            OleDbDataReader childNodesReader = childNodesCommand.ExecuteReader();

            if (childNodesReader.HasRows) {

                SiteMapNode childNode = null;
                while(childNodesReader.Read()) {
                    childNode =  new SiteMapNode(this,
                                                 childNodesReader.GetInt32(0).ToString(),
                                                 childNodesReader.GetString(1),
                                                 childNodesReader.GetString(2));

                    // Use the SiteMapNode AddNode method to add
                    // the SiteMapNode to the ChildNodes collection.
                    AddNode(childNode, rootNode);
                }
            }

            childNodesReader.Close();
            accessConnection.Close();
        }
        return rootNode;
    }
}

// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
public SiteMapNode BuildSiteMap() throws Exception
{
    // Since the SiteMap class is static, make sure that it is
    // not modified while the site map is built.
    synchronized (this)
    {
        // If there is no initialization, this method is being
        // called out of order.
        if (!get_IsInitialized())
        {
            throw new Exception("BuildSiteMap called incorrectly.");
        }
        // If there is no root node, then there is no site map.
        if (null == rootNode)
        {
            // Start with a clean slate
            Clear();
            // Select the root node of the site map from Microsoft Access.
            int rootNodeId = -1;

            if (accessConnection.get_State().Equals(ConnectionState.Closed))
            {
                accessConnection.Open();
            }

            OleDbCommand rootNodeCommand = new OleDbCommand(
                "SELECT nodeid, url, name FROM SiteMap WHERE "
                + "parentnodeid IS NULL", accessConnection);
            OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();

            if (rootNodeReader.get_HasRows())
            {
                rootNodeReader.Read();
                rootNodeId = rootNodeReader.GetInt32(0);
                // Create a SiteMapNode that references the current 
                // StaticSiteMapProvider.
                rootNode = new SiteMapNode(this,
                    ((Int32)rootNodeId).ToString(), rootNodeReader.
                    GetString(1), rootNodeReader.GetString(2));
            }
            else
            {
                return null;
            }
            rootNodeReader.Close();
            // Select the child nodes of the root node.
            OleDbCommand childNodesCommand = new OleDbCommand(
                "SELECT nodeid, url, name FROM SiteMap WHERE "
                + "parentnodeid = ?", accessConnection);
            OleDbParameter rootParam = new OleDbParameter("parentid",
                OleDbType.Integer);
            rootParam.set_Value((Int32)rootNodeId);
            childNodesCommand.get_Parameters().Add(rootParam);

            OleDbDataReader childNodesReader = childNodesCommand.
                ExecuteReader();

            if (childNodesReader.get_HasRows())
            {
                SiteMapNode childNode = null;
                while (childNodesReader.Read())
                {
                    childNode = new SiteMapNode(this,
                        Convert.ToString(childNodesReader.GetInt32(0)), 
                        childNodesReader.GetString(1),
                        childNodesReader.GetString(2));
                    // Use the SiteMapNode AddNode method to add
                    // the SiteMapNode to the ChildNodes collection.
                    AddNode(childNode, rootNode);
                }
            }

            childNodesReader.Close();
            accessConnection.Close();
        }
        return rootNode;
    }
} //BuildSiteMap

protected SiteMapNode GetRootNodeCore()
{
    return null;
} //GetRootNodeCore

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.