How to generate an Umbraco XML Sitemap dynamically

Today, I implemented an XML sitemap in my Umbraco 7.7.9 installation. Here are the things I had to do:

  1. Create a Sitemap document type
  2. Create a Sitemap macro
  3. Create a rule for IIS URL rewriting of the sitemap.xml
  4. Author the Sitemap content page

Sitemap document type

My document type involves two properties which will allow content authors to:

  • Exclude content from the sitemap
  • Hide page from the navigation – In Umbraco, if you want a document type to utilise the “IsVisible” property of IPublishedContent, you will need to create a property with the alias as “umbracoNaviHide”


My Sitemap document template view has the following code:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Sitemap>
@using ContentModels = Umbraco.Web.PublishedContentModels;
    Layout = null;
@Umbraco.RenderMacro("GetSitemap", new {})

Sitemap macro

The reason why I wanted to use a Macro is to utilise its caching features. I didn’t want to dynamically generate the sitemap on the fly. I only want it to dynamically generate once a day.

Note: I set my cache period to 86,400 seconds (equivalent to a day). No parameters are required


My macro view has the following implementation:

@inherits Umbraco.Web.Macros.PartialViewMacroPage
    var rootCache = UmbracoContext.Current.ContentCache.GetAtRoot();
    var exclusions = (List<IPublishedContent>)CurrentPage.Exclusions;
    var rootList = rootCache.Except(exclusions);
<urlset xmlns="">
    @foreach (var root in rootList)
            @if (root.HasValue("updateFrequency"))
        var descendants = root.Descendants().Except(exclusions);
        foreach (var content in descendants)
                @if (content.HasValue("updateFrequency"))

IIS Rewrite Rule

On the web.config, I added the following rewrite rule inside the <system.webServer>:

Note: The sitemapxml is the name I will use to create the Sitemap content page in the next step

      <rule name="Sitemap XML" enabled="true" stopProcessing="true">
         <match url="sitemap.xml" />
         <action type="Rewrite" url="sitemapxml" appendQueryString="false"/>

Sitemap Content Page

On the CMS Content, an author can now instantiate a Sitemap page:


Works on my Umbraco 7.7.9 🙂