Copied to clipboard

Flag this post as spam?

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


  • paco 25 posts 155 karma points
    Jul 22, 2020 @ 13:06
    paco
    0

    [SOLVED] - Sitemap xml issue in Umbraco 8.x multilingual site

    Hi all,

    I have a problem with dynamically-generated xml sitemap in an Umbraco 8.6.1 multilingual site (French and English Languages).

    I have adopted the new Umbraco 8.x multilanguage feature, as explained here: https://our.umbraco.com/documentation/Getting-Started/Backoffice/Variants/

    But, when I have developed the dynamically-generated xml sitemap, I noticed that, when a content was not published in all languages (e.g., in English but not in French), in sitemap xml nodes I have, e.g. for French contents, a link with a trailing "#" (like this, for example: https://www.mysite.com/fr/#).

    Here is the code:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using System.Configuration;
    @using Utility;
    @{Layout = null;
        var rootNode = Model.AncestorOrSelf(1);
        var language = UtilityProperty.GetCultureFromRequestUrl(Request.RawUrl);
        var rootNodeUrl = ConfigurationManager.AppSettings["UrlDomain"] + rootNode.Url(language);
    }<?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">
        @if(rootNode.IsPublished() && !rootNode.GetProperty("hideFromSitemap").Value<bool>())
        {
            <url>
                <loc>@rootNodeUrl</loc>
                <lastmod>@rootNode.UpdateDate.ToString("s")+00:00</lastmod>
                <changefreq>daily</changefreq>
                <priority>0.8</priority>
            </url>
        }
        @ListChildNodes(Model.AncestorOrSelf(1), language)
    </urlset>
    
    @helper ListChildNodes(IPublishedContent startNode, string language)
    {
        Response.ContentType = "text/xml";
        const int maxLevelForSiteMap = 100;
        string urlDomain = ConfigurationManager.AppSettings["UrlDomain"];
    
        string[] ignoredAliases = new string[] { "robots", "sitemapxml" };
    
        foreach (var node in startNode.Children
            .Where(n =>
                n.TemplateId > 0 && n.IsPublished()
                && (!n.HasProperty("hideFromSitemap") || !n.GetProperty("hideFromSitemap").Value<bool>()))
            .Select(n => n))
        {
            //excluding some contents, because they must not appear in XML
            if( !ignoredAliases.Contains(((IPublishedContent)node).ContentType.Alias.ToLower()) )
            {
                var nodeUrl = ((IPublishedContent)node).Url(language);
                if (nodeUrl != null && nodeUrl.Contains("http"))
                {
                    nodeUrl = nodeUrl.Replace("http://", "").Replace("https://", "");
                    nodeUrl = nodeUrl.Substring(nodeUrl.IndexOf("/"));
                }
                    var url = urlDomain + nodeUrl;
    
                            <url>
                                <loc>@url</loc>
                                <lastmod>@node.UpdateDate.ToString("s")+00:00</lastmod>
                                <changefreq>daily</changefreq>
                                <priority>0.8</priority>
                            </url>
    
                }
                if (node.Level <= maxLevelForSiteMap)
                 {
                        @ListChildNodes(node, language)
                 }
         }
    }
    

    How can I solve this problem?

    Thanks, Paco

  • paco 25 posts 155 karma points
    Jul 29, 2020 @ 11:19
    paco
    0

    Hi all, any idea?

  • Kevin Jump 2342 posts 14869 karma points MVP 8x c-trib
    Jul 29, 2020 @ 14:58
    Kevin Jump
    101

    Hi,

    i think you need to check if the content is published in the language you are building the site map for.

    so in your code, for getting the childNodes.

    n.TemplateId > 0 && n.IsPublished()
    

    becomes

    n.TemplateId > 0 && n.IsPublished(language) 
    

    then you should only get nodes that are published in the language, and they should have URLs.

Please Sign in or register to post replies

Write your reply to:

Draft