How to create a site map in Relatude

Many websites have a site map. The code below shows you how to create a simple site map.

Sitemap code

The example uses a literal control to hold the html for the sitemap:

 <h1>Sitemap</h1>
    <asp:Literal runat="server" ID="litSiteMap" EnableViewState="false"></asp:Literal>

The c# code

The code consists of a method that creates a StringBuilder, and fetches the root node. Then it calls a recursive method with the root node as one of the parameters. An int counter called level is used to make sure it doesn't call itself more than 10 times.

    protected void Page_Load(object sender, EventArgs e)
    {
        litSiteMap.Text = getSiteMapHtml();
    }


    string getSiteMapHtml() {
        StringBuilder html = new StringBuilder();
        HierarchicalContent hc = WAFContext.Session.GetContent<HierarchicalContent>(WAFContext.Session.SiteId);
        buildSiteMap(hc, 1, html);
        return html.ToString();
    }
    void buildSiteMap(HierarchicalContent parent, int level, StringBuilder html) {
        html.AppendLine("<ul>");        
        foreach (HierarchicalContent child in parent.Children.Query().Where(AqlHierarchicalContent.ShowInMenu == true).Execute()) {
            bool onSelectedBranch = child.IsParentOrSame(WAFContext.Request.NodeId);
            html.Append("<li class=\"Level");
            html.Append(level);            
            html.Append("\"><a href=\"");
            html.Append(HttpUtility.HtmlAttributeEncode(WAFContext.GetUrl(child.NodeId)));
            html.Append("\">");
            html.Append(HttpUtility.HtmlEncode(child.Name));
            html.Append("</a>");
            buildSiteMap(child, level + 1, html);
            html.Append("</li>");
        }
        html.AppendLine("</ul>");
    }

Result

The result when used on the basic demo site:

sitemap