SiteMap.RootNode SiteMap.RootNode SiteMap.RootNode SiteMap.RootNode Property

Definition

Gets a SiteMapNode object that represents the top-level page of the navigation structure for the site.

public:
 static property System::Web::SiteMapNode ^ RootNode { System::Web::SiteMapNode ^ get(); };
public static System.Web.SiteMapNode RootNode { get; }
member this.RootNode : System.Web.SiteMapNode
Public Shared ReadOnly Property RootNode As SiteMapNode

Property Value

A SiteMapNode that represents the top-level page of the site's navigation structure; otherwise, null, if security trimming is enabled and the node cannot be returned to the current user.

Exceptions

The site map feature is not enabled.

-or-

The RootNode resolves to null, which occurs if security trimming is enabled and the root node is not visible to the current user.

The feature is supported only when running in Low trust or higher.

Examples

The following code example demonstrates how to use the RootNode property to retrieve the SiteMapNode object that represents the root node of the site, regardless of the current location in the page hierarchy.

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    // Examine the CurrentNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.CurrentNode.Title + "<br />");
    Response.Write("<font COLOR='red'>" + SiteMap.CurrentNode.Url + "</font><br />");

    // What nodes are children of the CurrentNode?
    if (SiteMap.CurrentNode.HasChildNodes) {
        IEnumerator childNodesEnumerator = SiteMap.CurrentNode.ChildNodes.GetEnumerator();
        while (childNodesEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(childNodesEnumerator.Current.ToString() + "<br />");
        }
    }
    Response.Write("<hr />");

    // Examine the RootNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.RootNode.Title + "<br />");
    Response.Write(SiteMap.RootNode.Url + "<br />");

    // What nodes are children of the RootNode?
    if (SiteMap.RootNode.HasChildNodes) {
        IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
        while (rootNodesChildrenEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(rootNodesChildrenEnumerator.Current.ToString() + "<br />");
        }
    }
}
</script>
<script runat="server">

Private Sub Page_Load(sender As Object, e As EventArgs)

    ' Examine the CurrentNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.CurrentNode.Title & "<br />")
    Response.Write("<font COLOR='red'>" & SiteMap.CurrentNode.Url & "</font><br />")

    ' What nodes are children of the CurrentNode?
    If (SiteMap.CurrentNode.HasChildNodes) Then
        Dim ChildNodesEnumerator As IEnumerator = SiteMap.CurrentNode.ChildNodes.GetEnumerator()
        While (ChildNodesEnumerator.MoveNext())
            ' Prints the Title of each node.
            Response.Write(ChildNodesEnumerator.Current.ToString() & "<br />")
        End While
    End If
    Response.Write("<hr />")

    ' Examine the RootNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.RootNode.Title & "<br />")
    Response.Write(SiteMap.RootNode.Url & "<br />")

    ' What nodes are children of the RootNode?
    If (SiteMap.RootNode.HasChildNodes) Then
        Dim RootNodesChildrenEnumerator As IEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator()
        While (RootNodesChildrenEnumerator.MoveNext())
            ' Prints the Title of each node.
            Response.Write(RootNodesChildrenEnumerator.Current.ToString() & "<br />")
        End While
    End If

End Sub ' Page_Load
</script>

Remarks

The SiteMap class retrieves the RootNode property by requesting it from the provider. If there is a provider hierarchy, the SiteMap class retrieves the root node of the root provider in the hierarchy. To access the root node of the default provider, call the Provider property, which retrieves both the default provider and its RootNode property.

The XmlSiteMapProvider object, which is the default provider for the SiteMap class, supports only one RootNode.

If you implement your own SiteMapProvider class and override the abstract RootNode property, you must provide your own implementation to track the RootNode property of the SiteMapNode instance.

The root node must be visible to all users, by design. If the root node is not visible to all users when security trimming is enabled, accessing the RootNode property might result in an InvalidOperationException exception.

Applies to

See also