Generate a Sitefinity CMS sitemap using the API

This article provides a sample on how to generate Sitefinity sitemap using the API.

To generate sitemap for a specific site retrieving the site by its name.
The following code snippet uses MultisiteModule API to retrieve the current site:

C#
using Telerik.Sitefinity.Multisite;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.SitemapGenerator;

namespace SitefinityWebApp
{
   public class GenerateSitemap
   {
       //generate a sitemap for the current site
       public void GenerateSitemanAPI()
       {
           var multisiteManager = SystemManager.CurrentContext as MultisiteContext;
           var getSiteByName = multisiteManager.GetSiteByName("sitemapTest").Id;

           //schedule sitemap to execute immediately
           SitemapManager.GenerateSitemap(getSiteByName);
       }

       //generate  sitemap for all sites from a multisite project
       public void GenerateSitemapAllSites()
       {
           var multisiteManager = SystemManager.CurrentContext as MultisiteContext;
           foreach (var site in multisiteManager.GetSites())
           {
               SitemapManager.GenerateSitemap(site.Id);
           }
       }
   }
}

The second method loops trough all sites and to generate sitemap for all sites.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?