Instantly share code, notes, and snippets.

Embed
What would you like to do?
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