In this tutorial, you will learn how to create a sitemap.xml file within Umbraco 8. Introduced by Google in 2005, sitemap.xml ensures that all the content within your website is indexed correctly by a web crawler. When it comes to building a sitemap.xml file within Umbraco, most online tutorials recommend that you create a specific sitemap document type when building a sitemap.xml. I disagree with this approach. In my opinion, the downside of creating a specific document-type are:

  • A content editor can accidentally delete the page in the CMS breaking it

  • Sitemaps never change. Why make something configurable that does not change?

I think a better approach is to hide the sitemap from the editor. This can be done using a vanilla MVC controller instead. To do this, you will need to follow these steps:

  • Create an entry in the routing table.
  • Create a view model and a controller
  • Create a view
  • Create a URL Rewrite Rule

The first thing that you will need to do is create an entry in the routing table. When working with vanilla MVC in Umbraco the routing will not magically hook up. You will have to explicitly tell the application about this route. Without this step, the sitemap will simply 404 when you try to view it. In Umbraco V8 adding a route to the routing table, is done using a composer. See this article for more information on composers and components! The routing rule will look like this:

The rule will need to be wrapped in a component, which will need to be registered with a composer. As we are using MVC we will need a controller and a view. In order to follow good practices, we should always pass data between the two using a view model. Let us start by creating this model first:

Now, let us create the controller:

The code above gets the homepage, using GetByRoute("/"). We then iterate through all the children in order to build up the sitemap. Another thing to note, as we are using a vanilla MVC controller, do not forget to register the controllers within the dependency injection framework, SimpleInject. SimpleInject comes with Umbraco and without registering the controller within the container, you will encounter an error. To register stuff you again need to turn to a composer! Those things get everywhere!!! Again see the video linked above to learn how to do this.

Lastly, let us create a view to render out the sitemap.

This is everything you need. You should now see a sitemap when you navigate to /sitemap.xml. Enjoy!