Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sitefinitysteve/d0c360d9d969d422ce2d4854327f5b5a to your computer and use it in GitHub Desktop.
Save sitefinitysteve/d0c360d9d969d422ce2d4854327f5b5a to your computer and use it in GitHub Desktop.
Viewable sitemap
using System;
using System.Linq;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.SitemapGenerator.Abstractions.Events;
using Telerik.Sitefinity.SitemapGenerator.Data;
namespace SitefinityWebApp
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(this.Bootstrapper_Initialized);
}
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
if (e.CommandName == "Bootstrapped")
{
EventHub.Subscribe<ISitemapGeneratorBeforeWriting>(Before_Writing);
}
}
private void Before_Writing(ISitemapGeneratorBeforeWriting evt)
{
var currentContext = Telerik.Sitefinity.Services.SystemManager.CurrentHttpContext;
var host = currentContext.Request.Url.Host;
//Strip out trailing slashes and remove localhost
foreach (var e in evt.Entries)
{
e.Location = e.Location.TrimEnd('/').Replace("http://localhost", $"https://{host}");
}
//Dump to readable sitemap file
//https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap#expandable-3
try
{
string flatSitemap = "";
foreach (var e in evt.Entries.OrderBy(x => x.Location))
{
flatSitemap += $"{e.Location}\n";
}
File.WriteAllText(currentContext.Server.MapPath("~/sitemap.txt"), flatSitemap);
}
catch (Exception ex)
{
//Handle error?
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment