+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Getting your News articles full Urls

by Georgi Chokov
We have had a couple of requests lately on how can someone get the full urls of the news articles. There a quite a few reasons why you might need this list of urls - you might want to put those in a Google Sitemap or create some custom tracking of your Urls. In this blog post, we will explain how we could do that. The sample shows you how to work with Pages and Controls API, ContentFilters and modules Manager classes as well.

Alright, what is so special about these Urls?

First of all, the news articles urls depend on where the NewsView control is placed. Then, the NewsView control could use some filter in order to show only certain news articles. There is one more possibility - what if we have two news view controls, so the first one shows the list of items, while the second - the full article? Here is a sample code that considers the above concerns:

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
// 
using Telerik.News; 
using Telerik.Cms; 
using Telerik.News.WebControls; 
using System.Collections.Generic; 
using Telerik.Cms.Engine; 
using Telerik.Cms.Engine.ContentViewFiltering; 
 
public partial class FindNewsUrls : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        CmsManager pageManager = new CmsManager(); 
        IList pages = pageManager.GetPages(); 
        // 
        List<string> newsUrls = new List<string>(); 
        // 
        string vd = Request.AppRelativeCurrentExecutionFilePath.Replace("~", String.Empty); 
        string path = Request.Url.AbsoluteUri.Replace(vd, string.Empty); 
 
        foreach (IPage page in pages) 
        { 
            IList<ICmsWebControl> controls = page.Controls; 
            foreach (ICmsWebControl control in controls) 
            { 
                if (control.TypeName.Equals(typeof(Telerik.News.WebControls.NewsView).AssemblyQualifiedName)) 
                { 
                    NewsManager newsManager = new NewsManager("News"); 
                    IList newsArticles; 
                    NewsView nvCtrl = control.LoadControl() as NewsView; 
                    //load the "filtered" news articles only. Note that a blank FilterExpression is still a FilterExpression 
                    ContentFilterBuilder filterBuilder = new ContentFilterBuilder(nvCtrl); 
                    if (filterBuilder.IsFilterValid) 
                    { 
                        newsArticles = newsManager.Content.GetContent(0, 
                                          int.MaxValue, 
                                          nvCtrl.SortExpression, 
                                          filterBuilder.ParseTagFilter(), 
                                          ContentStatus.Published, 
                                          null
                                          filterBuilder.ParseParentsFilter(), 
                                          filterBuilder.ParseMetaFieldsFilter() 
                                          ); 
                    } 
                    else 
                    { 
                        //filter not valid. Perhaps the NewsView is showing..nothing. 
                        continue
                    } 
                    //We will take the URL from the Master control, so we could skip this NewsView if it is in 
                    //Detail behavior mode. 
                    if (nvCtrl.BehaviorMode == Telerik.Cms.Engine.WebControls.ContentView.BehaviorModes.Detail) 
                    { 
                        continue
                    } 
 
                    string pageUrl; 
                    //the news page could be the NewsView detail page... (e.g. NewsView is in Master/Auto mode) 
                    if (nvCtrl.SingleItemUrl.Length != 0) 
                    { 
                        pageUrl = nvCtrl.SingleItemUrl; 
                    } 
                    else 
                    { 
                        ICmsPage cmsPage = pageManager.GetPage(page.ID) as ICmsPage; 
                        //we could iterate through all additional urls, now taking the default one. 
                        pageUrl = cmsPage.DefaultUrl.Url; 
                    } 
 
                    pageUrl = pageUrl.Replace(".aspx"""); 
                    pageUrl = pageUrl.Replace("~"""); 
                    foreach (IContent article in newsArticles) 
                    { 
                        //this should be url = pageUrl + article.UrlWithExtension in 3.6 
                        string url = path + pageUrl + article.Url + ".aspx"
                        //add the url to the list 
                        if (!newsUrls.Contains(url)) 
                            newsUrls.Add(url); 
                    } 
                } 
            } 
 
        } 
        //show the urls
        foreach (string articleUrl in newsUrls) 
        { 
            Response.Write(articleUrl + "<br />"); 
        } 
    } 

I am sure that for the most of you, the code speaks for itself, but here are some notes on what it actually does:

1. Getting all pages.

2. Getting all controls from each page, and select only the NewsView controls

3. Creating a filter from the controls FilterExpression, so we could get the list of items that this control is showing.

4. Checking if there is Detail page specified

5. Building a list of items,  having in mind the current page url, the server url and the news article relative url.

If you want to make the things more complicated, you can play with the page additional urls, but if you include those urls in the Sitemap, do not forget to set them with the Canonical Url tag (everyone talks about SEO these days, even me .. sometimes :) )

This pretty much solves the problem. I am sure that you guys can optimize the code, but we want to illustrate the idea here. There might be other ways of getting the news articles urls - we would be glad to know about your idea.

2 comments

Leave a comment
  1. Gabe Sumner Jun 23, 2009
    I only briefly looked at this, but I'm guessing this same technique could be adapted for blogs.
  2. Georgi Jun 23, 2009
    Yes. You need to change the NewsManager with the BlogManager, and the NewsView with the BlogPosts only.
    This approach could be used for all Generic Content based modules.

    To make it work for all modules at once, you might use ContentManager class and iterate through all the providers. Then we will have a condition, to look up for the particular control type (NewsView,BlogPosts etc) depending on the current provider.

    Leave a comment