Building XML sitemap in Umbraco


Let’s see how we can build a nice XML sitemap using Umbraco. There are at least two ways to do it, first one, using Umbraco Published Content cache, and the second one, using Examine index.

The first solution uses only Razor view and it’s described very well at Dynamically generated XML Sitemaps with Umbraco 7 so I will focus on the second one.

In my opinion, Razor version is enough in most cases so if you don’t have any specific requirements you could stick with it.

Examine version is very useful though when there is a need for some advanced logic, like datatypes filtering, including media files in the XML sitemap, etc.

We will need three elements, Hijacked Umbraco Controller SitemapController, UmbracoSitemapService and SitemapItem.

As you can see I’m using searchPath field in the Examine query. This is not accessible by default and you have to create this field by your own. To do this you need to add OnGatheringNodeData method and attach it to the ‘GatherignNodeData` event.

Of course, you can put additional logic into the Examine query to filter out nodes you don’t want to include in the XML sitemap, but in this case, I will stay with the same conditions as in the example with Razor view.

The last step is to create Sitemap document type in your Umbraco Backoffice with empty Template named SitemapXml.

Little sum up

Although Examine version might look like overcomplicated it’s very useful in certain scenarios. It’s also slightly faster than Published Content cache version. I’ve run a small test on the website with 1500+ nodes and the sitemap was created in about 3.5s for the Razor version and in about 2.5s for Examine version.

Please let me know in comments if you have any additional questions, I will be happy to reply.

You can read more about Hijacking Umbraco Controllers in official documentation.

Having a nice project to code? // +48 606 115 330