Skip to content

Instantly share code, notes, and snippets.

@davidsekar
Created April 22, 2016 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidsekar/b433ec82fccb95db41984de3c51fa488 to your computer and use it in GitHub Desktop.
Save davidsekar/b433ec82fccb95db41984de3c51fa488 to your computer and use it in GitHub Desktop.
Sitefinity – Add auto generated product detailed page URLs to Sitemap XML by hooking to ISitemapGeneratorBeforeWriting
protected void Application_Start(object sender, EventArgs e)
{
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(this.Bootstrapper_Initialized);
}
private 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)
{
try
{
// sets the collection of entries to modified collection
evt.Entries = GenerateProductNodes(evt);
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
}
public static IEnumerable<SitemapEntry> GenerateProductNodes(ISitemapGeneratorBeforeWriting evt)
{
// gets the entries that are about to be written in the sitemap
var entries = evt.Entries.ToList();
var siteId = evt.SiteId;
ISite currentSiteContext = new MultisiteContext().GetSiteById(siteId);
string scheme = currentSiteContext.RequiresSsl ? "https" : "http";
string sSiteUrl = string.Format("{0}://{1}", scheme, currentSiteContext.LiveUrl);
var catalogProvider = currentSiteContext.GetDefaultProvider("Catalog");
string catalogProviderName = (catalogProvider != null) ? catalogProvider.ProviderName : CatalogManager.GetDefaultProviderName();
var catalogManager = CatalogManager.GetManager(catalogProviderName);
var products = catalogManager.GetProducts()
.Where(p => p.Status == ContentLifecycleStatus.Live).ToList();
CultureInfo defaultCulture = new CultureInfo(currentSiteContext.DefaultCulture);
var siteCultures = currentSiteContext.PublicContentCultures;
var clService = SystemManager.GetContentLocationService();
IContentItemLocation location;
foreach (var product in products)
{
if (product.AvailableCultures.Length <= 0) continue;
SitemapEntry productEntry = new SitemapEntry();
bool bAvailableInDefault = product.AvailableCultures.Contains(defaultCulture);
CultureInfo defaultEntryCulture = bAvailableInDefault ? defaultCulture : product.AvailableCultures[0];
location = clService.GetItemDefaultLocation(product, defaultEntryCulture);
productEntry.Location = string.Format("{0}{1}", sSiteUrl, new Uri(location.ItemAbsoluteUrl).PathAndQuery);
productEntry.Priority = 1;
productEntry.LastModified = product.LastModified;
string type = product.GetType().Name;
Dictionary<string, string> altLink = new Dictionary<string, string>();
foreach (var culture in product.AvailableCultures)
{
if (string.IsNullOrEmpty(culture.Name))
continue;
//gets the item default location of a given item by itemId provided
location = clService.GetItemDefaultLocation(typeof(Product), catalogProviderName, product.Id, culture);
//gets the absolute url for the location
if (location != null)
altLink.Add(culture.Name, string.Format("{0}{1}", sSiteUrl, new Uri(location.ItemAbsoluteUrl).PathAndQuery));
}
productEntry.Rels = altLink;
entries.Add(productEntry);
}
return entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment