All Articles

Customizing the XML Sitemap for Sitecore SXA 10.0

Introduction

Sitemaps help search engine crawlers navigate your site and improve search engine optimization (SEO).

The Sitecore Experience Accelerator(SXA) offers an Out-of-the-box Sitemap XML module, which generates the Sitemap XML for each of your defined sites. An XML sitemap is created specifically for search engines to show details of the available pages in a website, their relative importance, and the frequency of content updates.

Configuration

You can configure the Sitemap XML for the SXA Site in the Settings item under the Site item.

The configurations available can be found in the Search Engines Sitemap section of the Settings item.

Search Engines Sitemap Section

You get three options under Sitemap Mode.

  1. Inactive

    Choosing this option will disable the Sitemap for the site.

  2. Stored in cache

    This option will save the Sitemap file generated in the cache. This option may be used if you are using Azure environments to host your Sitecore instance.

  3. Stored in file

    This option is recommended if your site is not going to change very often.

You can choose to include externals sitemaps as Key-Value pairs as well.

Also, you can have the Sitemap Index instead of the Sitemap.

The Robots.txt file will have a link to the Sitemap XML file by default.

Include/Exclude a page in the Sitemap XML

Go to the page item and scroll to the Sitemap settings section.

Sitemap Settings in Page Item

The below options can be used to exclude a page from the Sitemap XML.

  1. Set value of Change Frequency to ‘do not include’.
  2. Or, set the value of the Priority field to empty.

Customize the behavior of the Sitemap XML module for SXA

We can modify the behavior by overriding the SitemapGenerator service in the Sitecore.XA.Feature.SiteMetadata assembly.

  1. Create a class named CustomSitemapGenerator.cs.

    namespace Feature.SitemapExtension.Services
    {
        public class CustomSitemapGenerator : Sitecore.XA.Feature.SiteMetadata.Sitemap.SitemapGenerator
        {
            public CustomSitemapGenerator() : base()
            {
    
            }
            public CustomSitemapGenerator(XmlWriterSettings xmlWriterSettings) : base(xmlWriterSettings)
            {
    
            }
            public override string GenerateSitemap(Item homeItem, SitemapLinkOptions sitemapLinkOptions)
            {
                return GenerateSitemap(homeItem, null, sitemapLinkOptions);
            }
            public override string GenerateSitemap(Item homeItem, NameValueCollection externalSitemaps, SitemapLinkOptions sitemapLinkOptions)
            {
                // Custom logic goes here
            }
        }
    }

    To build the custom logic, I would recommend you to use tools such as dotPeek/ILSpy to go through the SitemapGenerator class in the Sitecore.XA.Feature.SiteMetadata assembly**.**

  2. Create a config file named Feature.SitemapExtension.Sitemap.config.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <services>
          <register
            serviceType="Sitecore.XA.Feature.SiteMetadata.Sitemap.ISitemapGenerator, Sitecore.XA.Feature.SiteMetadata"
            implementationType="Feature.SitemapExtension.Services.CustomSitemapGenerator, Feature.SitemapExtension" lifetime="Transient"
            patch:instead="register[@implementationType='Sitecore.XA.Feature.SiteMetadata.Sitemap.SitemapGenerator, Sitecore.XA.Feature.SiteMetadata']"/>
        </services>
      </sitecore>
    </configuration>

After publishing your changes to your Sitecore instance, you should see the changes reflecting in your Sitemap XML.

I would recommend using the OOTB code as much as possible and keep your changes to be minimal.

Please feel free to drop in your feedback/questions in the comments.

Happy Sitecoring!

Published Aug 31, 2021

Sitecore MVP Technology 2024-23. Web Developer with rich experience in Sitecore and ASP.NET MVC.