Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JustAGuyTryingToCodeSomething/0b7dd2fc4c12ea35886943b5dbf0cc50 to your computer and use it in GitHub Desktop.
Save JustAGuyTryingToCodeSomething/0b7dd2fc4c12ea35886943b5dbf0cc50 to your computer and use it in GitHub Desktop.
Umbraco dynamic sitemap .cshtml template view. Based on article and gist by Alex Lindgren: https://gist.github.com/alindgren/1439022194a472d83ddf with a few tweaks for Articulate Blog Posts, https and extra Sitemap protocol entries.
@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)
{
<url>
<loc>http://www.yourlandingpage.com/</loc>
<lastmod>@(string.Format("{0:s}+00:00", startNode.UpdateDate))</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
foreach (var node in startNode.Children.Where("hideInXmlSitemap == false"))
{
if (node.TemplateId > 0 || node.DocumentTypeAlias == "ArticulateMarkdown" || node.DocumentTypeAlias == "ArticulateRichText")
{
<url>
<loc>@GetUrlWithDomainPrefix(node.Url)</loc>
<lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
<priority>0.4</priority>
<changefreq>monthly</changefreq>
</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("https://{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