Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 453 posts 840 karma points c-trib
    Feb 13, 2012 @ 07:21
    Murray Roke
    0

    Filter links by Template (or doc type)

    Thanks for the cool package.

    I modified the razor to filter by template. It could be tweaked to do doc types instead if you want.

    @using umbraco.cms.businesslogic.template
    @{ 
    var templatesNotToRender = new[]
                                {
                                    Template.GetByAlias("Non Page").Id
                                }; 
    }
    
    @ListChildNodes(Model.AncestorOrSelf(1), templatesNotToRender)
    
    @helper ListChildNodes(dynamic startNode,int[] templatesNotToRender)
    {
    
        Response.ContentType = "text/xml";
        const int maxLevelForSiteMap = 100;
    
        foreach (var node in startNode.Children
            .Where("HasAccess")
            .Where("!IsProtected")
            .Where("umbracoNaviHide == false")
          )
        {     
            if(node.template > 0 &&  ! templatesNotToRender.Contains((int)node.template)) {
                <url>
                    <loc>@GetUrlWithDomainPrefix(node.Url)</loc>
                    <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
                    @if (node.SearchEngineSitemapChangeFreq.ToString() != "")
                    {
                        <changefreq>@node.SearchEngineSitemapChangeFreq</changefreq>
                    }
                    @if (node.SearchEngineSitemapPriority.ToString() != "")
                    {
                        <priority>@node.SearchEngineSitemapPriority</priority>
                    }
                </url>
            }
    
            if (node.Level <= maxLevelForSiteMap && node.ChildrenAsList.Count > 0)
            {
                @ListChildNodes(node, templatesNotToRender)
            } 
        }
    }
    
    @functions {
        private static string GetUrlWithDomainPrefix(string url) {
            if (url.StartsWith("/"))
              url = url.Substring(1);
    
            var domainPrefix = string.Format("http://{0}/", HttpContext.Current.Request.ServerVariables["HTTP_HOST"]);
    
            if (url.StartsWith(domainPrefix))
                return url;
            else
                return domainPrefix + url;
        }
    }
Please Sign in or register to post replies

Write your reply to:

Draft