Copied to clipboard

Flag this post as spam?

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


  • Steve Morgan 1349 posts 4459 karma points c-trib
    Aug 18, 2014 @ 17:12
    Steve Morgan
    0

    Sitemap with DocType Exclusions List - umbracoNaviHide not excluded

    I've patched together a few examples (including the very useful CultivSearchEngineSitemap) to make a sitemap that allows me to exclude docs by their type (I have a list of excluded types and am using a lamda expression to filter this. 

    I added umbracoNaviHide to my master doc type but not all doc types are under this and to stop an exception being thrown I've added a check. The issue is that the umbracoNaviHide".Value != "1" doesn't seem to work - the pages where this true/false value is set still show in the sitemap output. Any ideas why?

    I also failed in trying to find a way of excluding protected pages - this isn't important for this project but any pointers on this would be useful. 

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        Response.ContentType = "text/xml";
        var homepageContainerId = 1060;
        string[] excludeTypes = { "settings", "Historyitem"};
    
        var homepage = Umbraco.TypedContent(homepageContainerId);
        if (homepageContainerId != null)
        {
            @:<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
            // pages contains only bona fide pages - e.g. not content nodes that are just used to build up other pages 
            var pages = homepage.DescendantsOrSelf().Where(x => (!x.HasProperty("umbracoNaviHide") || (x.HasProperty("umbracoNaviHide") && x.GetProperty("umbracoNaviHide").Value != "1")) && !excludeTypes.Contains(x.DocumentTypeAlias));
    
            foreach (var page in pages)
            {
                <url>
                <testonly>@page.Name</testonly>
                <loc>@GetUrlWithDomainPrefix(page.Url)</loc>
                <lastmod>@(string.Format("{0:s}+00:00", page.UpdateDate))</lastmod>
    
                @if (page.HasValue("SearchEngineSitemapChangeFreq"))
                {
                    <changefreq>@page.GetProperty("SearchEngineSitemapChangeFreq").Value</changefreq>
                }
                else
                {
                    <changefreq>yearly</changefreq>
                }
    
                @if (page.HasValue("SearchEngineSitemapPriority"))
                {
                    <priority>@page.GetProperty("SearchEngineSitemapPriority").Value</priority>
                }
                else
                {
                    <priority>0.5</priority>
                }
                @if (page.HasProperty("umbracoNaviHide")) { 
                    <ishiddenTEST>@page.GetProperty("umbracoNaviHide").Value</ishiddenTEST>
                }
                </url>
            }
            @:</urlset>
        }
    }
    
    @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;
        }
    }
  • Steve Morgan 1349 posts 4459 karma points c-trib
    Aug 18, 2014 @ 17:28
    Steve Morgan
    0

    Just to highlight the problem area it's probably here:

    var pages = homepage.DescendantsOrSelf().Where(x =>(!x.HasProperty("umbracoNaviHide")||(x.HasProperty("umbracoNaviHide")&& x.GetProperty("umbracoNaviHide").Value!="1"))&&!excludeTypes.Contains(x.DocumentTypeAlias));
Please Sign in or register to post replies

Write your reply to:

Draft