Copied to clipboard

Flag this post as spam?

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


  • Sagar 74 posts 275 karma points
    Jun 02, 2016 @ 05:28
    Sagar
    0

    XML Sitemap

    Hii Guys,

    How to do XML Sitemap in Umbraco.

  • Manish 373 posts 932 karma points
    Jun 02, 2016 @ 06:12
    Manish
    1

    Hi Sagar

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System.Linq;
    @{
        Layout = null;
        Response.ContentType = "text/xml";
    }<?xml version='1.0' encoding='UTF-8' ?>
    <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">
        @ListChildNodes(Umbraco.TypedContent(UmbracoContext.Current.PageId).AncestorOrSelf(1))
    </urlset>
    
    @helper ListChildNodes(IPublishedContent startNode)
    {
    
        foreach (var node in startNode.Children.Where("hideInXmlSitemap == false"))
        {
            if (node.TemplateId > 0 && node.DocumentTypeAlias != "DocTypeWhichYouDOn'tNeed" )
            {
                <url>
                    <loc>@GetUrlWithDomainPrefix(node.Url)</loc>
                    <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
                </url>
            }
    
            if (node.Level <= 100 && node.Children.Count() > 0)
            {
                @ListChildNodes(node)
            }
        }
    }
    
    @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;
        }
    }
    

    Here I have created one property in CMS as true/false (if you don't want some page as a part of sitemap just tick this).

    enter image description here

    Well this property is optional if you need all nodes to be listed in this you can skip this one Manish

  • Sagar 74 posts 275 karma points
    Jun 04, 2016 @ 05:48
    Sagar
    0

    Hii Manish I tried the Code which You Provided but..

    it does not Works with /sitemap but works fine with /XMLSitemap

    and in my firefox browser the sitemap is shown in Text File

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jun 02, 2016 @ 06:13
    Dennis Adolfi
    1

    Hi Sagar.

    The quickest and easiest way is to just install a XML Sitemap package like these:

    https://our.umbraco.org/projects/website-utilities/cultiv-search-engine-sitemap/

    https://our.umbraco.org/projects/collaboration/search-engine-sitemap/

    Most of them starts working as soon as you´d installed them.

    Best of luck to you!!

  • NK 8 posts 78 karma points
    Apr 17, 2018 @ 19:46
    NK
    0

    Tried both of these and they do not work; the sitemaps are blank. I wish people would include better installation instructions. Not everybody using Umbraco just "knows" what to do.

  • Sagar 74 posts 275 karma points
    Jun 02, 2016 @ 09:11
    Sagar
    102

    Thanxx Dennis and Manish i had already implemented it..

    thnxx for the reply...

    Here is My Code

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    @{
        umbraco.library.ChangeContentType("text/xml");
        int level = 6;
        var home = @CurrentPage.AncestorOrSelf(1);
        var pages = @home.Descendants().Where("hideInSitemap == false");
    }
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url> 
            <loc>@Request.Url.Scheme://@Request.Url.Host</loc>
            <lastmod>@home.UpdateDate.ToString("yyyy-MM-ddTHH:mm:00")+00:00</lastmod>
            <changefreq>daily</changefreq>
            <priority>0.8</priority>
        </url>
    
        @foreach (var page in pages)
        {
            if (page.Level <= @level)
            { 
                <url>
                    <loc>@Request.Url.Scheme://@[email protected]</loc>
                    <lastmod>@page.UpdateDate.ToString("yyyy-MM-ddTHH:mm:00")+00:00</lastmod>
                    <changefreq>weekly</changefreq>
                    <priority>0.5</priority>
                </url>
            }
        }
    </urlset>
    
  • NK 8 posts 78 karma points
    Apr 17, 2018 @ 19:45
    NK
    0

    I am trying to implement this and the sitemap only shows the homepage. I added your code as a template named Sitemap, created a Document Type with the same name, and published content with the same name. Am I doing it incorrectly?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jun 02, 2016 @ 09:17
    Dennis Adolfi
    1

    Great job Sagar!! Have a great day!

    Please mark your own post as solution so that others who are struggling with this may find your solution quicker.

  • Sagar 74 posts 275 karma points
    Jun 02, 2016 @ 09:57
    Sagar
    1

    Thnxx Dennis

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jun 02, 2016 @ 10:02
    Dennis Adolfi
    1

    No problem! Best of luck with the rest of the site!!

  • Sagar 74 posts 275 karma points
    Jun 04, 2016 @ 08:44
    Sagar
    0

    Sitemap not working properly on Firefox

Please Sign in or register to post replies

Write your reply to:

Draft