Menu

XML Sitemap with Umbraco

February 9, 2015 by Christopher Sherman

A sitemap is a file informing search engines about the organization of website content, according to Google. While web crawlers typically discover most content without a sitemap, however, there are specific instances where having a sitemap can improve discoverability.

In this tutorial, I’ll walk you through creating a XML sitemap with the Umbraco CMS. XML is the format search engines typically prefer for sitemaps. The steps that follow are modified from Jeavon Leopold’s instructions on the Our Umbraco forum. If you need to create a sitemap for human visitors, see my post on creating a HTML sitemap with Umbraco.

In Umbraco back office, navigate to Settings from the main menu. Hover over Partial Views, create a new partial view named XmlSitemap. In the Template field of the new view, paste the code below. This code grabs your home page, ensures it’s not null and has child content, and then prints the XML according to the nodes it finds.

Next, let’s add a view to the Templates section similar to how we created the partial view in the previous step. Name the template SitemapAsXml and use the following code in the template field to render the partial view we created earlier.

As Jeavon points out in the forum, we could create a node with the partial template and it would render the associated markup. Howver, we want the content type in the response header to be text/xml. This requires a custom controller known as a surface controller in Umbraco terminology.

If you have a Visual Studio project, add a Controllers folder, if it doesn’t already exist, and then add a class named XmlSurfaceController.cs to this folder. If you aren’t using Visual Studio, you can place the controller file in the App_Code directory. The controller should look like the code below (be sure to change the namespace).

Finally, add an IIS rewrite rule to your Web.config so the server knows where to find the template when it receives requests for /sitemap.xml. Note that the URL Rewrite extension must be installed on IIS for this to work. If you’re using Azure Websites for hosting, at the time of this post the rewrite extension is installed by default. You should add the configuration below to the system.webServer section of Web.config, omitting the system.webServer and rewrite tags if they’re already present.

Check that your sitemap is working by navigating to http://your-domain.com/sitemap.xml.

Umbraco SEO