Skip to content

Instantly share code, notes, and snippets.

@biapar
Forked from alindgren/XMLSitemap.cshtml
Created October 10, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biapar/bc40fe2682d9f450a053 to your computer and use it in GitHub Desktop.
Save biapar/bc40fe2682d9f450a053 to your computer and use it in GitHub Desktop.
XML sitemap for Umbraco 7 (based on Cultiv Search Engine Sitemap package). See http://www.alexlindgren.com/archive/dynamically-generated-xml-sitemaps-with-umbraco-7/
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Linq;
@{
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">
@ListChildNodes(Umbraco.TypedContent(UmbracoContext.Current.PageId).AncestorOrSelf(1))
</urlset>
@helper ListChildNodes(IPublishedContent startNode)
{
foreach (var node in startNode.Children.Where("hideInXmlSitemap == false"))
{
if (node.TemplateId > 0)
{
<url>
<loc>@GetUrlWithDomainPrefix(node.Url)</loc>
<lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
</url>
}
if (node.Level <= 100 && node.Children.Count() > 0)
{
@ListChildNodes(node)
}
}
}
@functions {
private static string GetUrlWithDomainPrefix(string url)
{
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment