Skip to content

Instantly share code, notes, and snippets.

@pbres
Created January 21, 2018 15:35
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 pbres/6016f1d1ded597836f96dec5f8933ffc to your computer and use it in GitHub Desktop.
Save pbres/6016f1d1ded597836f96dec5f8933ffc to your computer and use it in GitHub Desktop.
public class SitemapController : RenderMvcController
{
public ActionResult SitemapXml()
{
var startNode = CurrentPage.Site();
if (startNode != null)
{
var sitemapService = new UmbracoSitemapService();
var items = sitemapService.GetItemsAsList(startNode.Id);
XNamespace xn = "http://www.sitemaps.org/schemas/sitemap/0.9";
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
var urlSet = new XElement(xn + "urlset");
urlSet.Add(new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"));
foreach (var item in items)
{
urlSet.Add(new XElement(xn + "url", new XElement(xn + "loc", item.Url),
new XElement(xn + "lastmod", item.LastUpdateDate)));
}
doc.Add(urlSet);
return Content(String.Concat(doc.Declaration.ToString(), "\r\n", doc.ToString()), "text/xml");
}
return new HttpNotFoundResult("400");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment