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

Forums / Developing with Sitefinity / Custom Implementation of NewsView and NewsArchive for SEF URLs

Custom Implementation of NewsView and NewsArchive for SEF URLs

8 posts, 2 answered
  1. IH
    IH avatar
    11 posts
    Registered:
    13 Jan 2010
    15 Jan 2010
    Link to this post
    Hello,

    I am trying to implement SEF URLs compatibility in NewsView and NewsArchive controls by inheriting them in my own classes.

    1.
    With regards to NewsView control, it presently generate URLs like (suppose i have news.aspx which has the customnewsview control):

    /news/<newstitle>.aspx

    where as I wish to generate /news/<category>/<newstitle>.aspx

    Questions:
    a. How can I tell NewsView to generate the URLs in above manner?
    b. If the urls were generated in this way, I am sure I will also need to *listen* to the URLs in my own custom way (i.e. to interpret /news/category/title.aspx to the article itself) How/where in which method I would do that overriding/using which properties?

    2.
    How can I do the same with categories view. i.e. by default they are:
    /news.aspx?NewsCatID=xxxx
    and making them search engine friendly url, I want:
    /news/category/<categoryname>.aspx

    Questions:
    a. How can I tell NewsView to generate the URLs in above manner for a category?
    b. If the urls were generated in this way, I am sure I will also need to *listen* to the URLs in my own custom way (i.e. to interpret /news/category/categoryname.aspx to the category itself) How/where in which method I would do that overriding/using which properties?

    3.
    With regards to NewsArchive control, it presently generate URLs like (suppose i have news.aspx which has the customnewsarchive control):

    /news.aspx?Year=2009&Month=11 for November 2009 (which is so not search engine friendly)

    where as I wish to generate something like /news/archive/2009/november.aspx

    Questions:
    a. How can I tell NewsArchive to generate the URLs in above manner for a category?
    b. If the urls were generated in this way, I am sure I will also need to *listen* to the URLs in my own custom way (i.e. to interpret /news/archive/year/month.aspx to the month's archive itself) How/where in which method I would do that overriding/using which properties?

    With these lots of questions and the industry demand of SEF stuff, I am sure you guys will now be seriously considering to implement it in next version of site finity :) Anyway I have tried best to be clear in explaining the requirements, If you have any more questions please do let me know.

    Thanks
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    15 Jan 2010
    Link to this post
    Hi IH,

    Thank you for using our services.

    I believe that your requirements could be achive without providing custom implementation for the NewsView or the NewsArchive control. First I will ask you to take a look at the following webinar: URL Rewriting In Sitefinity, and the notes to the webinar.

    1) NewsView: you can provide your own url rewrite rules in the web.config in order to rewrite the urls in the way need. For example you can use:
    <add name="News" urlRewriteFormat="[Category]/[Title].aspx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Title" securityProviderName="" allowVersioning="True" applicationName="/News" versioningProviderName="" commentsModeration="true" connectionStringName="GenericContentConnection" type="Telerik.News.Data.DefaultNewsProvider, Telerik.News.Data"/>

    Which will produce the exact result you are looking for. This will work even for your CustomNewsView since it inherits from NewsView.

    2) Open the NewsView control and go to Advanced->QueryString Keys section and set the CategoryKeyType property to Name and modify the CategoryKey as you wish, refer to attached image. Which will produce urls like this: news.aspx?NewsCatName=Category. Then you can use the built in UrlRewriter as suggested in the webinar to form rewriting rules.

    3) The same can be achieved for the archive you will give rules when you see the ?Year=2009&Month=11 query string to rewrite the url to produce the desired urls.

    If you go down this way you will not have to develop custom controls and you will not have to listen for the urls being requested.

    Best wishes,
    Radoslav Georgiev
    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.
    Answered
  3. IH
    IH avatar
    11 posts
    Registered:
    13 Jan 2010
    15 Jan 2010
    Link to this post
    Hi Radoslav

    It all worked like a star! Thank you.

    For some custom implementation, I must override CreateDataSource of NewsView in my class.
    I am noticing that the filters built on NewsView control when an Item is clicked NewsArchive item - is messed up in my case.

    This is what I am doing now:


    Dim
     manager As New ContentManager("News")  
    manager.FilterExpression = Me.FilterExpression ' This usually gives me Publication_Date <= "#now" AND Expiration_Date > "#now"   
                                                   '(i guess thats for not to display expired and future news) 


    When I click on November 2009 on NewsArchive control, the Me.FilterExpression on NewsView control contains


    Publication_Date <= 
    "#1/15/2010 8:25:49 PM#" AND Expiration_Date > "#1/15/2010 8:25:49 PM#" AND Publication_Date >= "11/1/2009 12:00:00 AM" AND Publication_Date <= "11/30/2009 11:59:59 PM"  




    So when this line was ran


    manager.GetContent(
    "Publication_Date DESC"


    it gave me error:


    Invalid field name "Publication_Date 
    <= "#1/15/2010 8:27:39 PM#" AND Expiration_Date > "#1/15/2010 8:27:39 PM#" AND Publication_Date".   
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.   
     
    Exception Details: System.ArgumentException: Invalid field name "Publication_Date <= "#1/15/2010 8:27:39 PM#" AND Expiration_Date > "#1/15/2010 8:27:39 PM#" AND Publication_Date".  

     

    After spending a few hours, I found out that the error is coming from a missing a few hash (#) signs in the above Me.FilterExpression (i.e. it does'nt wrap the 3rd and 4th dates in #) -- Or maybe something else?

    My question is since I must override CreateDataSource and produce IList using manager.GetContent(), how do I apply correct value to manager.FilterExpression from the current context.

    Thanks
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    15 Jan 2010
    Link to this post
    Hello IH,

    Thank you for getting back to me.

    I am glad I was able to help on the first one. As to this issue - the problem is not in the missing hash characters, it seams that when the GetContent method is called some of the quotation marks in the filter expression are not escaped properly. Here is a another way of using the filter expression. This way you are building an array of filters, so you can even remove some of them at run time:
    'use ContentFilterBuilder to get the filters 
    'the filter builder gets a refference to the custom news view control
    'and is able to return an array of the applied filters
    Dim filterBuilder As New Telerik.Cms.Engine.ContentViewFiltering.ContentFilterBuilder(Me)
    'this returns the filters
    Dim filters As Telerik.Cms.Engine.IMetaSearchInfo() = filterBuilder.ParseMetaFieldsFilter()
    'create a new content manager for news provider
    Dim contentManager As Telerik.Cms.Engine.ContentManager = New ContentManager("News")
    'get content based on sort expression and the filters we have just parsed
    Dim news As IList = contentManager.GetContent("Publication_Date DESC", filters)

    You can check this link for information on available GetContent() overloads.
     
    Kind regards,
    Radoslav Georgiev
    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.
    Answered
  5. IH
    IH avatar
    11 posts
    Registered:
    13 Jan 2010
    15 Jan 2010
    Link to this post
    Hi Radoslav,

    I was able to achieve almost everything from your hints, thank you.

    There's a small problem that the URL for categories having space are decoded to %20, i.e. http://localhost:39887/news/category/category%20name

    which is produced by my custom implementation, i.e.
    Protected Overrides Function GetCategoryUrl(ByVal categoryID As System.Guid, ByVal singleItemUrl As StringAs String 
                Dim s As String = MyBase.GetCategoryUrl(categoryID, singleItemUrl)  
                Return GetSEFURLForCategory(s)  
    End Function 
    Function GetSEFURLForCategory(ByVal str As StringAs String 
                Dim s1() As String = str.Split("?")  
                Dim categoryName As String = "" 
                If s1.Length = 2 Then 
                    Dim s2() As String = s1(1).Split("=")  
                    If s2.Length = 2 Then 
                        If s2(0) = "NewsCatName" Then 
                            categoryName = s2(1)  
                        End If 
                    End If 
     
                End If 
                Dim ret As String = "" 
                If categoryName.Length > 0 Then 
                    ret = categoryName  
                End If 
            
                If ret.Length > 0 Then 
                    Dim origUrl As String = s1(0)  
                    origUrl = Left(origUrl, origUrl.Length - origUrl.LastIndexOf(".aspx"))  
                    Return origUrl & "/category/" & ret  
                Else 
                    Return str  
                End If 
     
    End Function 
     
     
     

    which is then translated by urlrewrite rule:
    <rule> 
        <url>/news/category/(.*)</url> 
        <rewrite>/news.aspx?NewsCatName=$1</rewrite> 
    </rule> 

    Which is pretty much fine. But If i tell method GetSEFURLForCategory to replace all <spaces> with <hyphens>, I will have no way to translate the category-name back to "category name" as it is automatically read by newsview control.

    Can you please propose a solution in which i can translate the passed "category-name" to "category<space>name" (the orignal) so that newsivew only shows news from that category.

    FYI i have implemented the archives/newsarchive as well as they're working good.

    Thank you

  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    18 Jan 2010
    Link to this post
    Hi IH,

    Could you try the slightly re-worked override of CreateDataSource method. We parse the categories filter using the filter builder class. If we have categories filter you should check if names are valid and rework them if necessary then clean filters in the filter builder and re-add the filters:
    Protected Overloads Overrides Function CreateDataSource() As System.Collections.IList
        'use ContentFilterBuilder to get the filters 
        'the filter builder gets a refference to the custom news view control
        'and is able to return an array of the applied filters
        Dim filterBuilder As New Telerik.Cms.Engine.ContentViewFiltering.ContentFilterBuilder(Me)
        'this returns the filters
        Dim filters As Telerik.Cms.Engine.IMetaSearchInfo() = filterBuilder.ParseMetaFieldsFilter()
        Dim categories As String() = filterBuilder.ParseCategories()
        If categories.Length > 0 Then
            For Each filter As MetaSearchInfo In filters
                If filter.Key = "Category" Then
                    'add logic for translating <space> characters
                    'should get filter.Value and translate then create new filter
                    Dim newCategoryFilter As New MetaSearchInfo(filter.ValueType, filter.Key, "NewValueForCategory", filter.Condition, filter.JoinType)
                    filterBuilder.RemoveFilter(filter)
                    filterBuilder.AddFilter(newCategoryFilter)
                End If
            Next
        End If
        'create a new content manager for news provider
        Dim contentManager As Telerik.Cms.Engine.ContentManager = New ContentManager("News")
        'get content based on sort expression and the filters we have just parsed
        Dim news As IList = contentManager.GetContent("Publication_Date DESC", filters)
        ' IList list = this.manager.GetContent();
        Return MyBase.CreateDataSource()
    End Function

    Kind regards,
    Radoslav Georgiev
    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.
  7. Daniel Plomp
    Daniel Plomp avatar
    112 posts
    Registered:
    14 Jun 2012
    07 Jun 2011
    Link to this post
    I know this is an old post, but I am in need of this functionality:

    - rewrite url's for a custom module.

    I created a custom module based on the products module. (serves companies)
    I want to filter companies based on categories.

    I placed a CategoryList on my page that generates a list of Categories for my custom provider.
    The url that is generated:
    http://www.mycompany.com/companies?CatName=Administration

    I want that url to be like this
    http://www.mycompany.com/companies/categories/administration

    I created the following UrlRewrite rule:
    <urlrewrites>
          <!-- Add your rule elements here -->
          <rule>
            <url>/companies/categories/(.*)</url>
            <rewrite>/companies?CatName=$1</rewrite>
          </rule>
        </urlrewrites>

    Unfortunately nothing happens when clicking a category.

    What did I forgot?

    Regards,
    Daniel

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    09 Jun 2011
    Link to this post
    Hello Daniel ,

    The expression does not match the url

    companies\?CatName\=test

    will match

    /companies?CatName=test

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
8 posts, 2 answered