Using An Agent To Generate A Sitecore Sitemap

Overview of setup & configuration

Learn How To Set Up A Task Agent To Generate A Sitemap

With Sitecore 10 and SXA this is less of an issue than in previous iterations. Aside from regenerating the sitemap every time that you publish a page (which we may cover in another article), what if you generated it on a schedule, say, every 24 hrs.

One way we can accomplish this is by setting up a task agent. This is different than setting up tasks within Content Editor. I'm referring to the ones set up in the Sitecore configuration itself.

Setup And Configuration

Part of what we might want is control over what gets included in the sitemap. This could be not only template type but also perhaps what we want to exclude. If we have multiple sites, one aspect of this Item might be the selection of the site (or sites) to include in the sitemap.

Creating an item template to track all that information is simple enough and not something we're going to go in-depth in today.

Within a patch file, we can create our agents to kick off at specific times.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="" xmlns:xdt="">
        <agent name="WWWSiteMap" type="ABCCompany.Feature.Sitemap.Tasks.SitemapTask,ABCCompany.Feature.Sitemap" method="Run" interval="12:00:00" patch:after="agent[@type='Sitecore.Tasks.CompactClientDataAgent']">

Here are some important parts of our configuration.

  • name - Used to ensure our agent is unique. Theoretically, if we had a dozen sites, we could use the same commands, just set-up a new agent.
  • type - Points to our sitemap task class.
  • method - Which method to use in said class.
  • interval - How often to run said method.
  • patch:after - Where to add the configuration.
  • SiteMapId - The GUID of the item that contains our sitemap configuration referenced above.

The Code

With the configuration in place, we now need code to kick off the sitemap generation.

namespace ABCCompany.Feature.Sitemap.Tasks
    class SitemapTask
        private ISitemapRepository _repo;

        // Same name as in the configuration file to capture what is passed to the agent
        public string SiteMapId { get; set; }

        public void Run()
            if (String.IsNullOrEmpty(this.SiteMapId)) return;

            // Get our Sitemap configuration item
            var db = Sitecore.Data.Database.GetDatabase("web");            
            var source = db.GetItem(new Sitecore.Data.ID(this.SiteMapId));
            if (source == null) return;
            // setup Sitemap repo
            _repo = new SitemapRepository();
            // Generate and store our sitemap.


What happens within _repo.CdreateSitemapFile(source) really depends on your site, what configurations you're looking to control, etc.

For us, it was important to generate an actual sitemap.xml file and that's something that occurs in this function. This way it wasn't trying to generate the sitemap on a specific web call. If your site is over a few hundred items, I strongly recommend against trying to generate the sitemap on-demand.

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting

Meet David Austin

Development Team Lead | Sitecore Technology MVP x 3


David is a decorated Development Team Lead with Sitecore Technology MVP and Coveo MVP awards, as well as Sitecore CDP & Personalize Certified. He's worked in IT for 25 years; everything ranging from Developer to Business Analyst to Group Lead helping manage everything from Intranet and Internet sites to facility management and application support. David is a dedicated family man who loves to spend time with his girls. He's also an avid photographer and loves to explore new places.

Connect with David