Generate Sitemap using Umbraco 6 CMS and ASP.NET MVC Razor code
Sitemap is still considered one of the most important element of Website development. Google and Bing do provide an option to reference your website sitemap so that their bots can traverse through all the links. But, most developers or website administrators often forget to include it. In this blog post, I will explain the steps to create a Sitemap using Umbraco 6, ASP.NET Partial View and Razor code. This article assumes that you have Umbraco 6 configured to use Mvc as the default rendering engine so that MVC Partial Views can be used.
Our sample Content hierarchy is as below:
Content - Home - Solutions - Products - Contact Us - Sitemap
Getting Started
We need to include all child items under parent menu item (Home) and create a sitemap dynamically.
In Umbraco CMS administration tool, click on Settings section and right-click on Document Types to create a new document named as Sitemap. Let Master Document Type be set to None… and Create matching template checked. Now click on Sitemap document type and go to Structure tab and set the checkbox for Allow at root option. Make sure that the Allow at root is checked on Home document type.
Also, you can add umbracoNaviHide property under Generic properties tab for base document type used by Home and all child pages within it. If this property is checked then a link to that page will not be included in the sitemap.
Copy-paste the code from Listing 1 to Sitemap template. Refer Sitemap XML protocol to understand the format of XML required by search engines.
Listing 1: Sitemap.cshtml
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = null; Response.ContentType = "text/xml"; }<?xml version='1.0' encoding='UTF-8'?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> @Html.Partial("GenerateSitemap", @Model.Content) </urlset>
Next step is to right-click on Partial Views and create a new partial view named as GenerateSitemap. This will bring up an editor. Copy-paste the code from Listing 2 below into the editor and click Save icon to save the sitemap partial view.
Listing 2: GenerateSitemap.cshtml
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent> @{ Layout = null; var parent = Model.Previous().AncestorOrSelf(1); if(@parent.Name== "Home") { <url> <loc>@GetUrlWithDomainPrefix(parent.Url, parent.Name)</loc> <lastmod>@(string.Format("{0:s}+00:00", parent.UpdateDate))</lastmod> </url> @ListChildNodes(parent); } } @helper ListChildNodes(IPublishedContent startNode) { const int maxLevelForSiteMap = 100; foreach (var node in startNode.Children.Where(x => x.IsVisible())) { <url> <loc>@GetUrlWithDomainPrefix(node.Url, node.Name)</loc> <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod> </url> if (node.Level <= maxLevelForSiteMap ) { @ListChildNodes(node) } } } @functions { private static string GetUrlWithDomainPrefix(string url, string name) { if (url.StartsWith("/")) url = url.Substring(1); var domainPrefix = string.Format("http://{0}/", HttpContext.Current.Request.ServerVariables["HTTP_HOST"]); if (url.StartsWith(domainPrefix)) return url; else return domainPrefix + url; } }
By default, partial views in Umbraco 6 inherits from the Umbraco.Web.Mvc.UmbracoViewPage class as shown on line 1 above.
The statement on line 4, var parent = Model.Previous().AncestorOrSelf(1) allows us to go one node back and traverse through all child items under Content tree-view. This code will work recursively based on the number of level it must traverse to generate the sitemap.
Create Sitemap Link
Now the final step is to create a content for sitemap from Content section. Right-click on Content and click on Create to enter name as Sitemap and Choose Document Type as Sitemap. Click on the Sitemap content to know the link for sitemap. Do remember to submit this link to Google and Bing so that your website gets indexed. You can also include this link in robots.txt file.
I hope you find this blog post helpful in increasing your website page rank.