public ActionResult XmlSitemap() { ContentHelper helper = this.GetContentHelper(); Page home = helper.GetRootNode(); if (home == null) { throw new NullReferenceException("Home page not found in content tree."); } XmlSitemap viewModel = new XmlSitemap(); try { viewModel.Items.Add(new SitemapItem(home.UrlAbsolute(), home.UpdateDate, home.ChangeFrequency, home.Priority)); } catch (Exception ex) { LogHelper.Error <ZoombracoXmlSitemapController>($"Cannot add node with id {home.Id} to the XML sitemap.", ex); } this.AddChildren(helper, home, viewModel); // If any handlers have been added for things like virtual nodes process them. ProcessXmlSitemapEventHandler handler = OnProcessXmlSitemap; handler?.Invoke(this, viewModel); return(this.View("XmlSitemap", viewModel)); }
/// <summary> /// Adds any child items that have the "ExcludeFromXmlSitemap" and "UmbracoNaviHide" properties set /// set to false. /// </summary> /// <param name="helper">The content helper.</param> /// <param name="parent">The parent content.</param> /// <param name="viewModel">The <see cref="XmlSitemap"/> to add to.</param> /// <param name="depth"> /// The recursion depth. /// </param> private void AddChildren(ContentHelper helper, Page parent, XmlSitemap viewModel, int depth = 0) { // Our recursion limit if (depth > 10) { return; } IEnumerable <Page> children = helper.GetChildren(parent.Id); foreach (Page child in children) { try { if (this.IncludeInSiteMap(child)) { viewModel.Items.Add(new SitemapItem(child.UrlAbsolute(), child.UpdateDate, child.ChangeFrequency, child.Priority)); this.AddChildren(helper, child, viewModel, depth + 1); } } catch (Exception ex) { LogHelper.Error <ZoombracoXmlSitemapController>($"Cannot add node with id {child.Id} to the XML sitemap.", ex); } } }
public ActionResult Index() { var model = new XmlSitemap(); int homeId = ContentHelper.Instance.GetHomeId(); Page home = ContentHelper.Instance.GetHome <Page>(); model.Items.Add(new SitemapItem(home.UrlAbsolute(), home.UpdateDate, home.ChangeFrequency, home.Priority)); this.AddChildren(home, model); return(this.View("XmlSitemap", model)); }
private void AddChildren(Page parent, XmlSitemap viewModel, int depth = 0) { // Our recursion limit if (depth > 10) { return; } IEnumerable <Page> childrens = parent.Children <Page>(); foreach (Page child in childrens) { if (!child.ExcludeFromXmlSitemap) { SitemapItem item = new SitemapItem(child.UrlAbsolute(), child.UpdateDate, child.ChangeFrequency, child.Priority); viewModel.Items.Add(item); this.AddChildren(child, viewModel, depth + 1); } } }