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

Forums / Developing with Sitefinity / Adding blog post urls to Google Sitemap

Adding blog post urls to Google Sitemap

18 posts, 0 answered
  1. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    13 Aug 2009
    Link to this post
    Hi there,

    I am using the following code to generate a Google sitemap. It works great, unfortunately i can not find the original forum thread i found this on so please forgive me for the lack of credit.

    <%@ 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 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 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; 
            } 
        } 

    As you can see i have commented the news portion of the code as i do not have news. I do however have blogs and would like to Append the blog post urls to the sitemap. Could someone perhaps help me figure out how to go about doing this and possibly provide me with some sample code?

    Thanks,
    Duncan
  2. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    13 Aug 2009
    Link to this post
    I have done this. Here is the code:

    <%@ 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; 
            } 
        } 

  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    14 Aug 2009
    Link to this post
    Hi Duncan Evans,

    It's great you've managed to accomplish this. I am sure that a lot of people will benefit from the code.

    We have updated your Telerik account points for posting the code.

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  4. terrysmith
    terrysmith avatar
    11 posts
    Registered:
    15 Nov 2006
    15 Sep 2009
    Link to this post
    Thanks for the code!

    I have one question though. If I uncomment the AppendNews section I get an error on this line saying that Common is not found:

            IPage page = new CmsManager().GetPage(Common.NewsPageViewerPageUid);

    Where is the Common class? I'm not familiar with the Sitefinity object model yet. I've looked for as well as searched online but can not find it.

    Thank you,
    Terry
  5. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    16 Sep 2009
    Link to this post
    Hi Terry,

    Honestly i have no idea what "common" is, like i said i found this code originally somewhere and have been adding to it ever since!

    I don't use News too much, this is why i just commented it out and moved on. But Because i lover this code that much, here is what i would do with the AppendNews method. Please let me knwo if this works as, again, i do not have much news items to test this with so i did this blind:

    1     private void AppendNews(XmlTextWriter writer) 
    2     { 
    3         string url = string.Empty; 
    4         string lastModified = string.Empty; 
    5         string changeFrequency = string.Empty; 
    6         string priority = string.Empty; 
    7  
    8         Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News"); 
    9  
    10         IList listOfNewsItems = newsManager.Content.GetContent(); 
    11  
    12         if (listOfNewsItems.Count > 0) 
    13         { 
    14             foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems) 
    15             { 
    16                 string NewsPagePath = "/NewsListPage/NewsArticlePage"//You could put this is the web.config to make this more dynamic 
    17  
    18                 url = GetURL(NewsPagePath + newsItem.UrlWithExtension); 
    19                 lastModified = GetLastModifiedDate(newsItem.DateModified); 
    20                 changeFrequency = GetChangeFrequency(newsItem.DateModified); 
    21                 priority = "0.5"
    22  
    23                 AppendUrlElement(writer, url, lastModified, changeFrequency, priority); 
    24  
    25             } 
    26         } 
    27     } 

    NewsPagePath would be the path to the page that holds your news control... You could make this more dymaic i guess by putting this value in the web.config or looking up the page somehow. But hard coding the path here should work for now...

    Let me know how it goes...

    Duncan
  6. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    16 Sep 2009
    Link to this post
    Full code with news:

    <%@ 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) 
        { 
            string url = string.Empty; 
            string lastModified = string.Empty; 
            string changeFrequency = string.Empty; 
            string priority = string.Empty; 
     
            Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News"); 
     
            IList listOfNewsItems = newsManager.Content.GetContent(); 
     
            if (listOfNewsItems.Count > 0) 
            { 
                foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems) 
                { 
                    string NewsPagePath = "/NewsListPage/NewsArticlePage"//You could put this is the web.config to make this more dynamic 
     
                    url = GetURL(NewsPagePath + newsItem.UrlWithExtension); 
                    lastModified = GetLastModifiedDate(newsItem.DateModified); 
                    changeFrequency = GetChangeFrequency(newsItem.DateModified); 
                    priority = "0.5"
     
                    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) 
                { 
                    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; 
            } 
        } 

  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    17 Sep 2009
    Link to this post
    Hello Duncan Evans,

    Thanks for providing the Sitemap. We could create a Tool (class with ITool interface) and use this code there, then to post it in the Forums/Blogs. I am sure that it will be very helpful.

    Thank you once again!

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    17 Sep 2009
    Link to this post
    That would be awesome! The tool could allow us to select the news and blogs post pages, or turn on and off certain parts of the sitemap (like if you do not want to generate links for news or blogs)

    Additionally i wanted to extend this code to look at the "indexed" attribute of the page. If the page is set to NOT be indexed by the search engines then leave it out of the sitemap that is generated.

    I would be happy to muddle up a tool for this if you show me the correct implementation of the ITool interface, or i will eagerly await the tool in your blogs... Let me know..

    Duncan
  9. terrysmith
    terrysmith avatar
    11 posts
    Registered:
    15 Nov 2006
    17 Sep 2009
    Link to this post
    Duncan - Thanks for the help. I changed the code a bit to look for an application path, so that it works in both development mode and when deployed.

    Georgi - Anything you could do to build this functionality into Sitefinity would be greatly appreciated. I consider sitemaps very basic, required functionality these days. I think Sitefinity should have one included out of the box with all public pages included by default and with item-level options to hide items from the sitemap if desired.

         private void AppendNews(XmlTextWriter writer)  
         {  
             string url = string.Empty;  
             string lastModified = string.Empty;  
             string changeFrequency = string.Empty;  
             string priority = string.Empty;  
     
             Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News");  
     
             IList listOfNewsItems = newsManager.Content.GetContent();  
     
             if (listOfNewsItems.Count > 0)  
             {  
                 foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems)  
                 {  
                     StringBuilder newsPagePath = new StringBuilder();  
     
                     if (!HttpContext.Current.Request.ApplicationPath.Equals("/"))  
                     {  
                         newsPagePath.Append(HttpContext.Current.Request.ApplicationPath);  
                     }  
                       
                     newsPagePath.Append("/Company/News");  //You could put this is the web.config to make this more dynamic    
                     newsPagePath.Append(newsItem.UrlWithExtension);  
                       
                     url = GetURL(newsPagePath.ToString());  
                     lastModified = GetLastModifiedDate(newsItem.DateModified);  
                     changeFrequency = GetChangeFrequency(newsItem.DateModified);  
                     priority = "0.5";  
     
                     AppendUrlElement(writer, url, lastModified, changeFrequency, priority);  
                 }  
             }  
         }  
  10. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    17 Sep 2009
    Link to this post
    I could be wrong, but i believe Sitefinity 4.0 will have some sort of sitemap functionality. I remember reading this somewhere i think.

    Duncan
  11. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    18 Sep 2009
    Link to this post
    Hello Duncan Evans,

    Yes, you are right about that, but still the current solution is very good for the 3.x users. 

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  12. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    22 Sep 2009
    Link to this post
    Georgi,

    Could you show me the implementation of the ITool interface for this code? I would like to get a tool going for this.

    Duncan
  13. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    25 Sep 2009
    Link to this post
    Hi Duncan Evans,

    This is more for a Service than a Tool actually. The services should be implemented with IService interface - there is pretty nice article here - http://www.sitefinity.com/help/developer-manual/services-adding-new-service-implementing-custom-services.html

    The reason for this is that the tool is suitable for some task that you do sometimes, while the Service is something that is constantly running or available on demand (just like the RSS). That said, I imagine the Goolge Sitemaps as a service, with a switch (on/off) and checkboxes showing what is included in the Sitemap. 

    If you still want a tool though, I will provide you with some guidance on it as well. 

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  14. Michael
    Michael avatar
    12 posts
    Registered:
    13 Oct 2008
    02 Oct 2009
    Link to this post
    Has anyone updated this to exclude certain page groups (based on URL or page title)? What about via the "do not show in navigation" or other properties?
  15. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    05 Oct 2009
    Link to this post
    Hello Michael,

    Looking at the code, it seems it doesn't count the Show in Navigation property. You can do that in the following method though:
     {  
            // 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);  
                }  
            }  
        }  

    You have to add the condition to the highlighted statement. 

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  16. Michael
    Michael avatar
    12 posts
    Registered:
    13 Oct 2008
    05 Oct 2009
    Link to this post
    Thanks. I guess part of my question was what is the best way to accomplish this, code-wise.
  17. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    05 Oct 2009
    Link to this post
    Hello Michael,

    On the second look, I saw that the page Sitemap is respecting the Navigable property:
    private void AppendNode(CmsPage page, XmlTextWriter writer, int depth) 
    {    
    ...  
      foreach (CmsPage cmsPage in page.ChildPages) 
                { 
                    if (page.Navigable == true
                    { 
                        AppendNode(cmsPage, writer, subNodeDepth); 
                    } 
                } 

    As for excluding page groups, you should have user interface to select what to exclude. You can always hardcode the exclusion like this:
        private void AppendNode(CmsPage page, XmlTextWriter writer, int depth) 
        { 
                foreach (CmsPage cmsPage in page.ChildPages) 
                { 
                    if (page.Navigable == true
                    { 
                        if (!page.StaticUrl.Contains("/restricted/")) 
                            AppendNode(cmsPage, writer, subNodeDepth); 
                    } 
                } 
            } 

    Or do some other checks based on the page properties. Just type page. and check the properties in the intellisense. 

    I hope this helps!

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  18. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    25 Oct 2009
    Link to this post
    Update:

    Google does not like some pages being external redirects. The AppendNode method has been updated to ignore these page types:

    private void AppendNode(CmsPage page, XmlTextWriter writer, int depth) 
     
        try 
        { 
            // in to deep?  
            int subNodeDepth = 0; 
     
            if (page.PageType != CmsPageType.External) 
            { 
                // 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
                { 
                    if (cmsPage.PageType != CmsPageType.External) 
                    { 
                        AppendNode(cmsPage, writer, subNodeDepth); 
                    } 
                } 
            } 
        } 
        catch 
        { 
            // fail silently?  
        } 
Register for webinar
18 posts, 0 answered