Skip to content

Instantly share code, notes, and snippets.

@mrflo
Created May 30, 2022 08:26
Show Gist options
  • Save mrflo/ba788c25d149847240281831d3bbecba to your computer and use it in GitHub Desktop.
Save mrflo/ba788c25d149847240281831d3bbecba to your computer and use it in GitHub Desktop.
Simple XML SiteMap for Umbraco 9
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{ Layout = null;
Context.Response.ContentType = "text/xml";
}
<?xml version="1.0" encoding="UTF-8" ?>
@{ var selection = Model.Root(); }
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>@Model.Parent.Url()</loc>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
@* Render the sitemap by passing the root node to the traverse helper, below *@
@{ Traverse(selection); }
</urlset>
@* Helper method to traverse through all descendants *@
@{
void Traverse(IPublishedContent node)
{
const int maxLevelForSitemap = 4;
var selection = node.Children().Where(x => x.IsVisible() && x.Level <= maxLevelForSitemap).ToArray();
@* If any items are returned, render a list *@
if (selection.Length > 0)
{
foreach (var item in selection)
{
if (@item.Level == 3)
{
<url>
<loc>@item.Url()</loc>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
}
Traverse(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment