Copied to clipboard

Flag this post as spam?

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


  • Streety 350 posts 558 karma points
    Jan 05, 2017 @ 12:43
    Streety
    0

    Ameded to pickup recursive Change Frequency & Priority

    Made a slight change so that Change Frequency & Priority were enabled by default.

    Steps:

    Add two new properties at the root or similar on your site. Change Frequency & Priority

    ...with the values stated in the release notes. I used a standard dropdown list.

    In Umbraco set the default values in that high level node.

    Next...

    Update the Sitemap template cshtml file with this.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{Layout = null;}<?xml version="1.0" encoding="UTF-8" ?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
            xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
            xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
    @ListChildNodes(Model.Content.AncestorOrSelf(3))
    </urlset>
    
    @helper ListChildNodes(IPublishedContent startNode)
    {
        Response.ContentType = "text/xml";
        const int maxLevelForSiteMap = 100;
        foreach(var node in startNode.Children
                        .Where(n => n.TemplateId > 0 && !Umbraco.IsProtected(n.Path))
                        .Select(n => n.AsDynamic() )
                )
        {
        <url>
            <loc>@(((IPublishedContent)node).UrlWithDomain())</loc>
            <lastmod>@node.UpdateDate.ToString("s")+00:00</lastmod>
    
        @if (node._SearchEngineSitemapChangeFreq.ToString() != ""){<changefreq>@node._SearchEngineSitemapChangeFreq</changefreq>}
    
        @if (node._SearchEngineSitemapPriority.ToString() != ""){<priority>@node._SearchEngineSitemapPriority</priority>}
    </url>
        if (node.Level <= maxLevelForSiteMap)
        {
            @ListChildNodes(node)
        }
    }
    

    }

    That's it. Change freq and priority are added automatically with default values until changed in the actual content page.

    Of Note:

    The script will return nothing unless a template is assigned to the content.

    n.TemplateId > 0
    

    Which makes perfect sense, however please note that if you have content as children which DO have templates associated BUT the parent doesn't, the recurse ability will stop, and you will need to amend the script to check of children with templates first.

    many thanks for writing.

Please Sign in or register to post replies

Write your reply to:

Draft