Auto Sitecore Sitemap Generation Module

Almost for every website we need to create sitemap. Sitemap helps to let search engine know about the pages and site structure. For Sitecore website its very difficult to maintain the sitemap, because the content team can add / edit n-number(s) of pages into their site. So to manual maintenance is next to impossible. So use the below module you can let system know and generate automatic sitemap after certain interval of time or date. 

 

Auto Sitemap Generation Generates Sitemap for us on scheduled time and day in the week for the mentioned Domains. It checks in interval of 1 minute that is also mentioned. 
 

Config is added on location –  

App_Config\Include\zzz\SchedulingAgents.config 

 

Config Patch -  
 

<agent type="SitecoreTasks.GenerateSitemapAgent" method="Run" interval="00:01:00">     

<Enable>true</Enable> 

<StartHour>6:06 am</StartHour> 

<StartDayOfWeek>wednesday</StartDayOfWeek>   <DomainMappings>localsite|www.website.com|localsite</DomainMappings> 

</agent> 

</scheduling> 

Config Patch Explanation - 
 

  • type="SitecoreTasks.GenerateSitemapAgent" 
     
    This specifies the namespace(Sitecore Tasks) and This is the root node of the class(GenerateSitemanpAgent) where method to generate sitemap is present. 

Location - ProjectLibrary\SitecoreTasks\GenerateSitemapAgent.cs 

 

  • method="Run" 

 

This specifies the method name(Run) in the above mentioned class which is being called to Generate Sitemap. 

 

  • interval="00:01:00 

This is the time interval in which it checks if theSitemap needs to be Generated. Here since the time interval is one minute it checks every minute if the Sitemap has to be created. 

  • <DomainMappings> 

Inside Domain Mapping all the Domains(Microsite Domains) for which Sitemap needs to be created is present 
 

  • <Enable>true</Enable> 
     

We can enable or disable our scheduler from here. If it is true then according to the time the Sitemap will be Generated, but in case of False the scheduler is turned off. 

  • <StartHour>1:00 AM</StartHour> 

 

The time when we want sitemap generation to start. 

 

  • <StartDayOfWeek>wednesday</StartDayOfWeek 

 

The day of the week when we have to start generating the Sitemap. 

 

  

Code File is added on location – 

ProjectLibrary\SitecoreTasks\GenerateSitemapAgent.cs 
 
 #region Namespaces
using System;
using System.Web;
using System.Xml;
#endregion

namespace SitecoreTasks
{
/// <summary>
/// Used to generate auto generated sitemap
/// </summary>
public class GenerateSitemapAgent
{
#region Properties
/// <summary>
/// Set value on agent setting "true"/"false", to enable and disable the task
/// </summary>
public string Enable { get; set; }

/// <summary>
/// Used to configure the Start hour e.g 1:00 AM
/// </summary>
public string StartHour { get; set; }

/// <summary>
/// Used to configure the day to perform weekly task e.g sunday
/// </summary>
public string StartDayOfWeek { get; set; }

/// <summary>
/// Domain Mappings
/// </summary>
public string DomainMappings { get; set; }
#endregion

/// <summary>
/// </summary>
string DWebSite = System.Configuration.ConfigurationManager.AppSettings["DWebSite"];


#region Methods
/// <summary>
/// Public method to run the agent
/// </summary>
public void Run()
{
try
{
//Sitecore.Diagnostics.Log.Info("Sitemap Date Print Now " + System.DateTime.Now.ToShortTimeString().ToLower().Trim(), this);
//Sitecore.Diagnostics.Log.Info("Sitemap Date Print StartHour " + StartHour.ToLower().Trim(), this);
//Sitecore.Diagnostics.Log.Info("Sitemap Date Print DayOfWeek " + System.DateTime.Now.DayOfWeek.ToString().ToLower().Trim(), this);
//Sitecore.Diagnostics.Log.Info("Sitemap Date Print StartDayOfWeek " + StartDayOfWeek.ToLower().Trim(), this);

if (Enable.Trim().ToLower().Trim() == "true" && System.DateTime.Now.ToShortTimeString().ToLower().Trim() == StartHour.ToLower().Trim() ||
System.DateTime.Now.DayOfWeek.ToString().ToLower().Trim() == StartDayOfWeek.ToLower().Trim())
{
Sitecore.Diagnostics.Log.Info("Sitemap Generate has been started at : " + System.DateTime.Now.ToString(), this);

string SiteName = string.Empty;
string DomainName = string.Empty;
string localDomainName = string.Empty;
string sitemapdomain = DomainMappings;
string physicalWebRootPath = HttpRuntime.AppDomainAppPath + ("/sitemap.xml");

XmlTextWriter writer = new XmlTextWriter(physicalWebRootPath, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("sitemapindex");
writer.WriteAttributeString("xmlns:xhtml", "http://www.w3.org/1999/xhtml");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

foreach (string mappedsitedomain in sitemapdomain.Split(','))
{
SiteName = mappedsitedomain.Split('|')[0];
DomainName = mappedsitedomain.Split('|')[1];
localDomainName = mappedsitedomain.Split('|')[2];
//Sitemap.xml generateXml
string LAST_MODIFY = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
writer.WriteStartElement("sitemap");
if (SiteName.ToLower() == DWebSite)
{
writer.WriteElementString("loc", "https://" + DomainName + "/d_website_sitemap.xml");
}
writer.WriteElementString("lastmod", LAST_MODIFY);
writer.WriteEndElement();
}
writer.WriteEndDocument();
writer.Flush();

Sitecore.Diagnostics.Log.Info("Sitemap Generate has been completed at : " + System.DateTime.Now.ToString(), this);
}
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error : SitecoreTasks>> GenerateSitemapAgent >> Run()", ex, this);
}
}

#endregion
}
}

  
 

Comments

Popular posts from this blog

Sitecore Powershell Extension (SPE)