Is there a way to modify this code to allow the user to enter in a live website URL and have it run off of that? I have multiple Sitefinity websites and I would like to use this code from one central location instead of running it from within each of my sites.
<%@ WebHandler Language="C#" Class="SiteMap" %>
using System;
using System.Web;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Specialized;
using Telerik.Search;
using Telerik.Cms;
using Telerik.Cms.Data;
using Telerik;
using Telerik.Cms.Engine;
using Lucene.Net.Documents;
using Lucene.Net.Search;
public class SiteMap : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try {
HttpResponse response = context.Response;
response.ContentType = "text/plain";
XmlTextWriter writer = new XmlTextWriter(response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
GetCmsPages pages = new GetCmsPages();
pages.LoadAll();
//Use this part for your custom modules
//pages = AppendTestimonials(pages)
//pages = AppendNews(pages)
foreach (CmsPage page in pages) {
if (page.Status == PageStatus.Published == true) {
AppendNode(page, writer, 0);
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
response.Flush();
writer.Flush();
}
catch (Exception ex) {
}
}
public bool IsReusable {
get { return true; }
}
private void AppendNode(CmsPage page, XmlTextWriter writer, int depth)
{
try {
int subNodeDepth;
string pageURL = GetURL(page);
// Check to see if the URL contains "_test", skip it if it does.
if (pageURL.Contains("_test") == false) {
writer.WriteStartElement("url");
writer.WriteElementString("loc", pageURL);
writer.WriteElementString("lastmod", GetLastMod(page));
writer.WriteElementString("changefreq", GetChangeFreq(page));
writer.WriteElementString("priority", GetPriority(page));
writer.WriteEndElement();
subNodeDepth = depth + 1;
//Not needed because get all pages is called at the beginning.
foreach (CmsPage n in page.ChildPages) {
if (page.Navigable == true) {
AppendNode(n, writer, subNodeDepth);
} // End if
} // End foreach
} // End if
} // End try
catch (Exception ex) {
} // End catch
}
private string GetURL(CmsPage page)
{
try {
NameValueCollection vars = HttpContext.Current.Request.ServerVariables;
string port = vars["SERVER_PORT"].ToString();
string protocol = vars["SERVER_PORT_SECURE"].ToString();
string server = vars["SERVER_NAME"].ToString();
System.Web.UI.Control urlResolver = new System.Web.UI.Control();
string url;
if ((port == null | port == "80" | port == "443")) {
port = "";
}
else {
port = ":" + port;
}
if ((protocol == null | protocol == "0")) {
protocol = "http://";
}
else {
protocol = "https://";
}
url = page.DefaultUrl.Url;
return protocol + vars["SERVER_NAME"].ToString() + port + urlResolver.ResolveUrl(url);
}
catch (Exception ex) {
return string.Empty;
}
}
private string GetLastMod(CmsPage page)
{
try {
return String.Format("{0:yyyy-MM-ddThh:mm:sszzzz}", page.DateModified);
}
catch (Exception ex) {
return string.Empty;
}
}
private string GetChangeFreq(CmsPage page)
{
try {
string intervalString = string.Empty;
DateTime dtStart = page.DateModified;
DateTime dtEnd = DateTime.Now;
TimeSpan tx = dtEnd - dtStart;
long intervalLong = System.Convert.ToInt64(tx.TotalHours);
if (intervalLong < 1)
intervalString = "hourly";
else if (intervalLong <= 24 & intervalLong > 1)
intervalString = "daily";
else if (intervalLong <= 168 & intervalLong > 24)
intervalString = "weekly";
else if (intervalLong <= 672 & intervalLong > 168)
intervalString = "monthly";
else if (intervalLong <= 8766 & intervalLong > 672)
intervalString = "yearly";
return intervalString;
}
catch (Exception ex) {
return string.Empty;
}
}
private string GetPriority(CmsPage page)
{
try {
return "0.5";
}
catch (Exception ex) {
return string.Empty;
}
}
}