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.
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.
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.