Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Puvi 6 posts 26 karma points
    Sep 06, 2013 @ 16:12
    Puvi
    0

    Creating sitemap in HTML

    I have created new umbraco website with basic navigation. I want to create a sitemap link on the website. On click of the link , I want sitemap to be appeared in HTML format. Can you please help me giving, basic idea of how to create a sitemap. Thanks.

  • Flavio Spezi 129 posts 315 karma points
    Sep 13, 2013 @ 09:19
    Flavio Spezi
    0

    Hello @Puvi. To goal your target you must:

    • Obtain Home document, example: var homeNode = Model.Content.AncestorOrSelf(2);
    • Cycle into all children of Home document;
    • Repeat cycle for any level that you want to render.

    This razor code can help you:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @functions {
    
        private List<string> excludeDocTypes = new List<string>(new string[] { "LinkEsterno", "Progetto" });
    
    
        IHtmlString drawItem(int siblingCount, Umbraco.Core.Models.IPublishedContent item, string label, int level, bool showChildren) {
            if (excludeDocTypes.Contains(item.DocumentTypeAlias)) return null;
    
            string strUrl = Libreria.PageUrl(item);
            string htmlLabel = Html.Encode(label ?? Libreria.PageMenuName(item, false));
            var fReturn = new System.Text.StringBuilder();
    
            fReturn.Append("<li>");
            if (strUrl == null) {
                fReturn.Append("<a>" + htmlLabel + "</a>");
            } else {
                fReturn.Append("<a href=\"" + Html.Encode(strUrl) + "\" title=\"" + htmlLabel + "\">" + htmlLabel + "</a>");
            }
    
            if (showChildren) {
                fReturn.Append("<ul class=\"lst\">");
                var subChild = item.Children;
                foreach (var subitem in subChild ) {
                    fReturn.Append(drawItem(subChild .Count(), subitem, null, level + 1, false));
                }
                fReturn.Append("</ul>");
            }
            fReturn.Append("</li>");
    
            return Html.Raw(fReturn.ToString());
        }
    }
    
    @{
        Umbraco.Core.Models.IPublishedContent DocHome = Model.Content.AncestorOrSelf(2);
    
    
        var child = DocHome.Children;
        var menuCodes = "main head1 head2";
    
        int childCount = child.Where(n => menuCodes.IndexOf(n.GetPropertyValue<string>("inMenu") ?? "#empty", StringComparison.InvariantCulture) > -1 && excludeDocTypes.Contains(n.DocumentTypeAlias) == false).Count();
    
        <nav>
            <ul class="lst">
                @{
    
                    foreach (Umbraco.Core.Models.IPublishedContent item in child) {
                        if (menuCodes.IndexOf(item.GetPropertyValue<string>("inMenu") ?? "#empty", StringComparison.InvariantCulture) > -1) {
                            @drawItem(childCount, item, null, 1, true)
                        }
                    }
                }
            </ul>
        </nav>
    }
    
  • Puvi 6 posts 26 karma points
    Sep 16, 2013 @ 10:57
    Puvi
    0

    Thanks Flavio. will try this.

  • Flavio Spezi 129 posts 315 karma points
    Sep 16, 2013 @ 10:59
    Flavio Spezi
    0

    Ok, let me know if it works correctly.

  • Flavio Spezi 129 posts 315 karma points
    Sep 24, 2013 @ 18:05
    Flavio Spezi
    0

    Do you solved?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies