Getting your News articles full Urls

Getting your News articles full Urls

June 23, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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.

progress-logo

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar