Skip to content

Instantly share code, notes, and snippets.

@grumpycatsaysno
Created July 17, 2017 13:06
Show Gist options
  • Save grumpycatsaysno/8fc230424a411a2f1022c87f260b8fe2 to your computer and use it in GitHub Desktop.
Save grumpycatsaysno/8fc230424a411a2f1022c87f260b8fe2 to your computer and use it in GitHub Desktop.
private static void Before_Writing(ISitemapGeneratorBeforeWriting evt)
{
// gets the entries that are about to be written in the sitemap
var entries = evt.Entries.ToList();
// gets the full Url
string hostUrl = SystemManager.CurrentHttpContext.Request.Url.ToString();
// get all the blog posts
BlogsManager manager = BlogsManager.GetManager();
IQueryable<BlogPost> posts = manager.GetBlogPosts()
.Where(p => p.Status == ContentLifecycleStatus.Live && p.Visible == true);
// add blog posts pages to sitemap
if (blogPosts != null)
{
foreach (var bp in blogPosts)
{
// build the Url
string blogPostUrl = string.Format("{0}/{1}",hostUrl + BlogDetailUrl, bp.UrlName);
if (!string.IsNullOrEmpty(blogPostUrl))
{
// Create new sitemap entry for each custom url
SitemapEntry newSitemapEntry = new SitemapEntry();
// Set the entry location – (the <loc></loc> tag) - required
newSitemapEntry.Location = blogPostUrl;
// Set the other XML tags - optional
newSitemapEntry.Priority = 0.5F;
// Add this new entry to the list of entries to be written by//sitemap generator
entries.Add(newSitemapEntry);
}
}
}
evt.Entries = entries;
}
private const string BlogDetailUrl = "blog/post";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment