step‐by‐step guide to creating an XML sitemap in Umbraco 8 - FadiZahhar/umbraco8showandtell GitHub Wiki

🧰 Prerequisites

Ensure you have:

  • An Umbraco 8 project set up and running.
  • Access to the project's file system and the Umbraco backoffice.
  • Basic knowledge of Razor syntax and Umbraco's templating system.

📁 Step 1: Create the Sitemap Template

  1. In the Umbraco backoffice, navigate to Settings > Templates.
  2. Create a new template named XmlSitemap.
  3. Replace the default content with the following Razor code:([www.helloblake.com on Notion]1)
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Xml.Linq
@{
    Layout = null;
    Response.ContentType = "text/xml";
    var nodes = Umbraco.ContentAtRoot().DescendantsOrSelf().Where(x => x.IsVisible());
    var urlset = new XElement("urlset",
        new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
        new XAttribute(XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance") + "schemaLocation",
            "http://www.sitemaps.org/schemas/sitemap/0.9 " +
            "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
    );

    foreach (var node in nodes)
    {
        var url = new XElement("url",
            new XElement("loc", node.Url(mode: UrlMode.Absolute)),
            new XElement("lastmod", node.UpdateDate.ToString("yyyy-MM-ddTHH:mm:sszzz"))
        );
        urlset.Add(url);
    }

    var sitemap = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), urlset);
    @sitemap.Declaration
    @sitemap
}

This code:

  • Sets the response content type to XML.
  • Retrieves all visible content nodes.
  • Generates an XML sitemap with <loc> and <lastmod> elements for each node.([Stack Overflow]2, [Swimburger]3)

📄 Step 2: Create the Sitemap Document Type

  1. Navigate to Settings > Document Types.
  2. Create a new document type named Sitemap.
  3. Under the Templates tab, associate it with the XmlSitemap template you just created.
  4. Ensure this document type is allowed at the root level if you plan to place it at the root of your content tree.([Nicola Ayan]4, [v8.codeshare.co.uk]5)

🧱 Step 3: Create the Sitemap Content Node

  1. Navigate to Content.
  2. Create a new content node using the Sitemap document type.
  3. Name it sitemap (this will result in a URL like https://www.yoursite.com/sitemap/).
  4. Publish the node.([Gist]6, [Nicola Ayan]4, [docs.umbraco.com]7)

🔁 Step 4: Configure URL Rewriting (Optional)

To serve the sitemap at https://www.yoursite.com/sitemap.xml, you can set up a URL rewrite rule.

  1. Open your site's web.config file.
  2. Locate the <system.webServer> section.
  3. Add the following rewrite rule:([Nicola Ayan]4)
<rewrite>
  <rules>
    <rule name="Sitemap XML" stopProcessing="true">
      <match url="^sitemap\.xml$" />
      <action type="Rewrite" url="/sitemap/" />
    </rule>
  </rules>
</rewrite>

This rule rewrites requests from /sitemap.xml to /sitemap/, allowing search engines to access your sitemap at the standard URL.


✅ Step 5: Submit Your Sitemap to Search Engines


🛠️ Optional Enhancements

  • Exclude Specific Pages: Add a boolean property (e.g., excludeFromSitemap) to your document types. Modify the Razor code to filter out nodes where this property is true.([v8.codeshare.co.uk]5)

  • Multilingual Support: For multilingual sites, ensure your sitemap includes alternate URLs for each language variant using the <xhtml:link> tag.([Umbraco]9)

  • Caching: Implement caching to reduce server load, especially for large sites.


Feel free to customize the sitemap generation logic to fit your site's specific needs. If you need assistance with advanced features like handling multilingual content or integrating with SEO tools, don't hesitate to ask!

⚠️ **GitHub.com Fallback** ⚠️