, ,

Sitecore supports the management of multiple website in one CMS environment. All sites could have a different domain name, and the same Sitecore backend. This is very convenient, because it increases the reuse of controls for developers, and the reuse of content for content managers.

But because all sites have a unique domain name, they should also have a unique Sitemap.xml file for Search Engines. The Sitemap.xml file is submitted to Search Engine Robots to index the pages of the the site.

Writing a static XML-file that is updated every time a user saves or updates an item is not a good solution, because it unnecessarily hookes into the Sitecore pipeline. Better is to let the XML-document be constructed ‘on the fly’: at the moment that a Search Engine Robot needs it.

The best way to achieve this in an environment with a single website is to use a generic handler (.ashx). The handler could be used to send an XML response when a certain URL is requested. So the first step is to add a handler class, that implement IHttpHandler. The constuction of the XML-string itself is encapsulated in this example:

public class SitemapXmlHandler : IHttpHandler
	public void ProcessRequest(HttpContext context)
		ISitemapService sitemapService = new SitemapService();
		string sitemapXmlString = sitemapService.GenerateSitemapXml();
		context.Response.ContentType = "text/xml";
	public bool IsReusable
			return true;

The handler and the URL by which it could be requested can be configured in the section of the web.config. But then we run into a problem. Because the URL is configured in the web.config, the URL does not correspond to a Sitecore item. So it is also impossible to call Sitecore.Context.Item, and from the item deduce the current website for which the sitemap.xml should be constructed.

So we need a way to call this handler from a URL that corresponds with a Sitecore item. To achieve this, we first add a new Layout item to Sitecore. We point the physical path of the Layout not to an .aspx file, as usual, but to the .ashx handler.

Then we add a page under Sitecore that is based on this layout, named “Sitemap” for example. Now, if the URL http://mywebsite.com/Sitemap is requested, the handler is called and serves a nice Sitemap.xml. Happy Coding!

sitemap item layout details