More in this section

Forums / Developing with Sitefinity / Filtering news items by taxon programmatically

Filtering news items by taxon programmatically

9 posts, 0 answered
  1. Joao
    Joao avatar
    7 posts
    Registered:
    19 Jul 2011
    25 Aug 2011
    Link to this post
    Hello.

    I'm developing a custom user control to retrieve a list of recent news items that are tagged with a specific tag. I can't use any of the built-in Sitefinity controls because there is some special client-side functionality related to displaying the results (clicking on a banner shows/hides results via an accordion effect).

    On the .ascx page, I have placed a Repeater that simply displays a series of anchor tags.

    <asp:Repeater ID="rptMyRepeater" runat="server">
                <ItemTemplate>
                    <a href="<%#DataBinder.Eval(Container.DataItem, "Urls[0].Url")%>"><%#DataBinder.Eval(Container.DataItem, "Title")%></a>
                </ItemTemplate>
                <SeparatorTemplate><br /></SeparatorTemplate>
            </asp:Repeater>

    In the code-behind, I am trying to retrieve a list of news items from the DB:

    if (!Page.IsPostBack)
                {
                    TaxonomyManager manager = TaxonomyManager.GetManager();
                    var taxon = manager.GetTaxonomies<FlatTaxonomy>()
                                          .Where(t => t.Name == "mytag")
                                          .SingleOrDefault();

                    var content = GetAllNews(5, taxon);

                    rptMyRepeater.DataSource = content;
                    rptMyRepeater.DataBind();
     }
     
    public IQueryable<NewsItem> GetAllNews(int numberOfitems, FlatTaxonomy taxon)
            {
                NewsManager manager = NewsManager.GetManager();
                IQueryable<NewsItem> allNews = manager
                                .GetNewsItems()
             //.Where(n => n.Organizer.TaxonExists(taxon.Name, taxon.Id))
             .OrderByDescending(n => n.PublicationDate)
             .Take(numberOfitems)
             .Distinct();
     
                return allNews;
            }

    I'm having two problems with this code:
    - first, it's returning 5 results, even though I have only created 2 news items in total. Results are repeated.
    - second, if I uncomment the Where line, I get an error (see below).

    System.NotSupportedException: Execution of 'Telerik.Sitefinity.Taxonomies.Model.OrganizerBase:TaxonExists(String,Guid)' on the database server side currently not implemented.

    My questions:
    - how to get a series of distinct results, with no doubles ?
    - how to get the filtering by tag to work ?
    - how to broaden the search to all content types with this tag, instead of just news items ?

    Thanks...
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    26 Aug 2011
    Link to this post
    Hi Joao,

    You are getting five results because you a querying all news items (drafts, live and master). If you want only published you need to query only live items. You can check this thread for samples:
    http://www.sitefinity.com/devnet/forums/sitefinity-4-x/developing-with-sitefinity/newsitem-query.aspx

    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. Joao
    Joao avatar
    7 posts
    Registered:
    19 Jul 2011
    26 Aug 2011
    Link to this post
    Hi Radoslav,

    Thanks for your help. I've now solved the problem of repeated items by modifying my code to this:

    NewsManager manager = NewsManager.GetManager();
    IQueryable<NewsItem> allNews = manager.GetNewsItems()
                  .Where(n => n.Status == ContentLifecycleStatus.Live)
                  .OrderByDescending(n => n.PublicationDate)
                  .Take(numberOfitems);

    But I still have 2 issues:
    1) can't filter by tag - I couldn't extract a solution from the link you sent, because it's using the Fluent API, not a manager object, to access data
    2) would like to modify code to retrieve a mixed list of all sorts of content, not just news, from a single query.

    Thanks,
    João
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    26 Aug 2011
    Link to this post
    Hello Joao,

    1) It does not matter whether you use manager instance or Fluent API. Fluent API is a wrapper for the managers. GetValue method is an extension method. To gain access to it you need to reference Telerik.Sitefinity.Model namespace.

    2) This is not possible. Since all content is provider specific you can retrieve content only by provider.

    Greetings,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  5. Joao
    Joao avatar
    7 posts
    Registered:
    19 Jul 2011
    26 Aug 2011
    Link to this post
    Where is the guid for the tag obtained from? It does not seem to be available via the dashboard. In any case I would prefer retrieving it programmatically to hard-coding it on the page.

    This is the method I am using to retrieve the tag guid at the moment, but it is giving an error on the final line because the previous line returns null. I have tried both t.Name and t.TaxonName, but the error remains.

    TaxonomyManager manager = TaxonomyManager.GetManager();
                    var taxonid = manager.GetTaxonomies<FlatTaxonomy>()
                                          .Where(t => t.TaxonName == "meettheboss")
                                          .SingleOrDefault()
                                          .Id;

    And then I'm using it this way. This piece of code works as long as I pass it a valid guid. But I'm worried that "Category" should read "Tag" or something else.

    var allNews = App.WorkWith()
               .NewsItems()
               .Publihed()
               .Where(ci => ci.GetValue<IList<Guid>>("Category").Contains(taxonid))
               .Get();


  6. Joao
    Joao avatar
    7 posts
    Registered:
    19 Jul 2011
    26 Aug 2011
    Link to this post
    I've found the guid in the DB, inside the [sf_news_items_tags] table. I had hoped to do this programmatically, but I retrieved it manually so I could test the rest of the code.

    This is the line I'm testing:

    var mtbCategory = new Guid("2900B2CB-B53A-40FD-866E-AB1D8CF168F9");
                 var allNews = App.WorkWith()
                                 .NewsItems()
                                 .Publihed()
                                 .Where(ci => ci.GetValue<IList<Guid>>("Category").Contains(mtbCategory))
                                 .Get();

    There were no errors but it returned no results. I suspected this was because the guid I passed refers to a tag, not a category. So I tried this instead:

    .Where(ci => ci.GetValue<IList<Guid>>("Tag").Contains(mtbCategory))

    and got this error:

    System.ArgumentOutOfRangeException: Database mapped field does not exist.
    Parameter name: methodCallExpression
    Actual value was ci.FieldValue("Tag").

  7. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    26 Aug 2011
    Link to this post
    Hello Joao,

    Try with Tags as the property for flat taxons is a plural.

    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  8. Joao
    Joao avatar
    7 posts
    Registered:
    19 Jul 2011
    26 Aug 2011
    Link to this post
    Thank you for your help, I have finally got the code to work.

    Here is the full solution, for the record:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    var content = GetAllNews(5);
     
                    rpt.DataSource = content;
                    rpt.DataBind();
                }
            }
     
            public IQueryable<NewsItem> GetAllNews(int numberOfitems)
            {
                var mtbCategory = new Guid("2900B2CB-B53A-40FD-866E-AB1D8CF168F9");
     
                var allNews = App.WorkWith()
                        .NewsItems()
                         .Publihed()
                      .Where(ci => ci.GetValue<IList<Guid>>("Tags").Equals(mtbCategory))  
                       .Get();
     
                return allNews;
            }

    If you can indicate a method for retrieving the category or tag guid from the DB, based on the name, that would give me a complete solution. Thanks again.
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    30 Aug 2011
    Link to this post
    Hi Joao,

    As Georgi has noted in your other forum post, please use the TaxonomyManager to query for taxonomy items.

    All the best,
    Radoslav Georgiev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

9 posts, 0 answered