+1-888-365-2779
Try Now
More in this section

Forums / 3.x Pre-release forums (retired) / Sean Molam's sitemap.org for Sitefinity

Sean Molam's sitemap.org for Sitefinity

35 posts, 0 answered
  1. Richard
    Richard avatar
    16 posts
    Registered:
    14 Apr 2009
    21 Jul 2009
    Link to this post
    Thanks - not sure why I didn't do that in the first place! Think it was a long day. All works great now.
  2. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    05 Aug 2009
    Link to this post
    Matthew,

    Great code sample! I have implemented this and it works great for the most part, except after looking of the generated XML one of my pages is throwing this error:

    <url>
        <loc>http://localhost/ver3/OurPrograms/AlcoholRehabilitation.a<html>
        <head>
            <title>Server cannot modify cookies after HTTP headers have been sent.</title>
            <style>
             body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
    
    ...

    After which no more xml is generated. Any ideas what is happening here. This page is .aspx not .a as it says above...

    Thanks,
    Duncan
  3. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    05 Aug 2009
    Link to this post
    It seems to only do this in Firefox...
  4. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    13 Aug 2009
    Link to this post
    I have extended this to include all blog posts also:

    <%@ WebHandler Language="C#" Class="GoogleSiteMap" %> 
     
    using System; 
    using System.Web; 
    using System.IO; 
    using System.Text; 
    using System.Xml; 
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Collections.Specialized; 
     
    using Telerik.Search; 
    using Telerik.Cms; 
    using Telerik.Cms.Web; 
    using Telerik.Cms.Data; 
    using Telerik.Cms.Engine; 
    using Telerik; 
    using Telerik.News; 
    using Telerik.News.Data; 
    using Telerik.Sitefinity; 
     
     
    public class GoogleSiteMap : IHttpHandler 
        public void ProcessRequest(HttpContext context) 
        { 
            try 
            { 
                // the request  
                HttpResponse response = context.Response; 
                response.ContentType = "text/plain"
     
                // the writer to create the xml file in  
                XmlTextWriter writer = new XmlTextWriter(response.OutputStream, Encoding.UTF8); 
     
                // start writing  
                writer.Formatting = Formatting.Indented; 
                writer.WriteStartDocument(); 
                writer.WriteStartElement("urlset"); 
                writer.WriteAttributeString("xmlns""http://www.sitemaps.org/schemas/sitemap/0.9"); 
     
                // add news  
                //AppendNews(writer); 
     
                // add blogs  
                AppendBlogs(writer); 
     
                // add pages  
                AppendPages(writer); 
     
                // finish  
                writer.WriteEndElement(); 
                writer.WriteEndDocument(); 
     
                // and flush to client  
                response.Flush(); 
                writer.Flush(); 
            } 
            catch 
            { 
                throw
            } 
        } 
     
        public bool IsReusable 
        { 
            get { return true; } 
        } 
     
        private void AppendPages(XmlTextWriter writer) 
        { 
            // get a pages object  
            GetCmsPages pages = new GetCmsPages(); 
            // and load em all up  
            pages.LoadAll(); 
            // process each page  
            foreach (CmsPage page in pages) 
            { 
                if (page.Status == PageStatus.Published == true
                { 
                    AppendNode(page, writer, 0); 
                } 
            } 
        } 
     
        //private void AppendNews(XmlTextWriter writer) 
        //{ 
        //    // values we need  
        //    string url = string.Empty; 
        //    string lastModified = string.Empty; 
        //    string changeFrequency = string.Empty; 
        //    string priority = string.Empty; 
     
        //    // the news viewer page (this value is stored in the web.config appSettings )  
        //    IPage page = new CmsManager().GetPage(Common.NewsPageViewerPageUid); 
     
        //    // the url of the page (string off the .aspx extension)  
        //    string baseNewsViewerUrl = GetURL(((CmsPage)page).DefaultUrl.Url).Replace(".aspx", ""); 
     
        //    // get active news articles  
        //    IList articles = new NewsManager("News").Content.GetContent( 
        //            0, 
        //            0, 
        //            new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", DateTime.Now, SearchCondition.LessOrEqual), 
        //            new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterThen, JoinType.Or) 
        //        ); 
     
        //    // process each item          
        //    foreach (IContent article in articles) 
        //    { 
        //        // if not published then skip  
        //        if (article.Status != ContentStatus.Published) 
        //            continue; 
     
        //        url = string.Concat(baseNewsViewerUrl, article.Url.ToString()); 
        //        lastModified = GetLastModifiedDate(article.DateModified); 
        //        changeFrequency = GetChangeFrequency(article.DateModified); 
        //        // default on news pages  
        //        priority = "0.5"; 
     
        //        // add element to writer  
        //        AppendUrlElement(writer, url, lastModified, changeFrequency, priority); 
        //    } 
        //} 
     
        private void AppendBlogs(XmlTextWriter writer) 
        { 
            string url = string.Empty; 
            string lastModified = string.Empty; 
            string changeFrequency = string.Empty; 
            string priority = string.Empty; 
     
            Telerik.Blogs.BlogManager blogManager = new Telerik.Blogs.BlogManager(); 
     
            IList listOfAllBlogs = blogManager.GetBlogs("PostsCount ASC"); 
            if (listOfAllBlogs.Count > 0) 
            { 
                foreach (Telerik.Blogs.IBlog blogItem in listOfAllBlogs) 
                { 
     
                    //make sure to set the blogs (advanced setting) "Post page" url. 
                    string baseBlogsPageUrl = GetURL(blogItem.PostPage).Replace(".aspx"""); 
                     
                    IList listOfPosts = blogItem.Posts; 
                    if (listOfPosts.Count > 0) 
                    { 
                        foreach (Telerik.Cms.Engine.IContent blogPost in listOfPosts) 
                        { 
                            url = string.Concat(baseBlogsPageUrl, blogPost.UrlWithExtension); 
                            lastModified = GetLastModifiedDate(blogPost.DateModified); 
                            changeFrequency = GetChangeFrequency(blogPost.DateModified); 
                            priority = "0.5"
     
                            AppendUrlElement(writer, url, lastModified, changeFrequency, priority); 
                        } 
                    } 
                } 
            } 
        } 
     
        private void AppendNode(CmsPage page, XmlTextWriter writer, int depth) 
        { 
     
            try 
            { 
                // in to deep?  
                int subNodeDepth = 0; 
     
                // values we need  
                string url = GetURL(page.DefaultUrl.Url); 
                string lastModified = GetLastModifiedDate(page.DateModified); 
                string changeFrequency = GetChangeFrequency(page.DateModified); 
                // always return a max priority for the home page,   
                string priority = (page.ID == new UrlHelper.HomePageInfo().ID) ? "1.0" : GetPriority(page); 
     
                // add element to writer  
                AppendUrlElement(writer, url, lastModified, changeFrequency, priority); 
     
                // how deep can we go?  
                subNodeDepth = depth + 1; 
     
                // process each sub page  
                foreach (CmsPage cmsPage in page.ChildPages) 
                { 
                    if (page.Navigable == true
                    { 
                        AppendNode(cmsPage, writer, subNodeDepth); 
                    } 
                } 
            } 
            catch 
            { 
                // fail silently?  
            } 
        } 
     
        private void AppendUrlElement(XmlTextWriter writer, string url, string lastModified, string changeFrequency, string priority) 
        { 
            writer.WriteStartElement("url"); 
            writer.WriteElementString("loc", url); 
            writer.WriteElementString("lastmod", lastModified); 
            writer.WriteElementString("changefreq", changeFrequency); 
            writer.WriteElementString("priority", priority); 
            writer.WriteEndElement(); 
        } 
     
        private string GetURL(string url) 
        { 
            try 
            { 
                NameValueCollection vars = HttpContext.Current.Request.ServerVariables; 
                string port = vars["SERVER_PORT"]; 
                string protocol = vars["SERVER_PORT_SECURE"]; 
                System.Web.UI.Control urlResolver = new System.Web.UI.Control(); 
     
                if ((port == null | port == "80" | port == "443")) 
                { 
                    port = ""
                } 
                else 
                { 
                    port = ":" + port; 
                } 
     
                if ((protocol == null | protocol == "0")) 
                { 
                    protocol = "http://"
                } 
                else 
                { 
                    protocol = "https://"
                } 
     
                return protocol + vars["SERVER_NAME"].ToString() + port + urlResolver.ResolveUrl(url); 
            } 
            catch 
            { 
                return string.Empty; 
            } 
        } 
     
        private string GetLastModifiedDate(DateTime dateModified) 
        { 
            try 
            { 
                return dateModified.ToString("yyyy-MM-ddThh:mm:sszzzz"); 
            } 
            catch 
            { 
                return string.Empty; 
            } 
        } 
     
        private string GetChangeFrequency(DateTime lastModified) 
        { 
            string defaultValue = "weekly"
     
            try 
            { 
                int intervalLong = DateTime.Today.Subtract(lastModified).Hours; 
     
                if (intervalLong < 1) 
                    return "hourly"
                else if (intervalLong <= 24 & intervalLong > 1) 
                    return "daily"
                else if (intervalLong <= 168 & intervalLong > 24) 
                    return "weekly"
                else if (intervalLong <= 672 & intervalLong > 168) 
                    return "monthly"
                else if (intervalLong <= 8766 & intervalLong > 672) 
                    return "yearly"
     
                // here? default  
                return defaultValue; 
            } 
            catch 
            { 
                return defaultValue; 
            } 
        } 
     
        private string GetPriority(CmsPage page) 
        { 
            // if no specific value or an exception condition, retrun the default  
            string defaultValue = "0.5"
     
            try 
            { 
                foreach (HeaderControl headerControl in page.HeaderControls) 
                { 
                    foreach (CmsControlProperty controlProperty in headerControl.Properties) 
                    { 
                        if (controlProperty.Name == "Content" && !String.IsNullOrEmpty(controlProperty.Value)) 
                        { 
                            return controlProperty.Value; 
                        } 
                    } 
                } 
     
                return defaultValue; 
            } 
            catch 
            { 
                return defaultValue; 
            } 
        } 

  5. Greg Mattison
    Greg Mattison avatar
    15 posts
    Registered:
    14 Jul 2005
    24 May 2010
    Link to this post
    Hello,

    We've implemented the sitemap.ashx file several months ago on one site. Recently, I've installed it on a couple current sites.  I've submitted the sitemap.ashx file to Google, Bing and Yahoo as the site map in their Webmaster sections. It worked but threw a bunch of errors, three of the errors are below but the same error shows multiple times.  Actually, it says there are 180 pages but only indexed 50 of them.  

    You can see it here - http://www.impactgrouphr.com/sitemap.ashx.  In addition, I've attached a zip of the sitemap.ashx file. 

    If anyone can post or email me a working sitemap.ashx file, I'd greatly appreciate it. 

    Greg
    greg.mattison@habanero.com
    314-771-5522, ext 274 



    Line Status Details 
    5 Invalid date
    An invalid date was found. Please fix the date or formatting before resubmitting. Parent tag: url
    Tag: lastmod
    Value: 02009-06-17T09:27:59-05:00
    Problem detected on: Apr 30, 2010 

    11 Invalid date
    An invalid date was found. Please fix the date or formatting before resubmitting. Parent tag: url
    Tag: lastmod
    Value: 02009-07-30T02:42:20-05:00
    Problem detected on: Apr 30, 2010 

    17 Invalid date
    An invalid date was found. Please fix the date or formatting before resubmitting. Parent tag: url
    Tag: lastmod
    Value: 02010-01-28T09:46:22-06:00
    Problem detected on: Apr 30, 201
Register for webinar
35 posts, 0 answered
1 2