Filtering news items by taxon programmatically
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
>
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;
System.NotSupportedException: Execution of 'Telerik.Sitefinity.Taxonomies.Model.OrganizerBase:TaxonExists(String,Guid)' on the database server side currently not implemented.
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 >>
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);
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 >>
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.
var allNews = App.WorkWith()
.NewsItems()
.Publihed()
.Where(ci => ci.GetValue<
IList
<Guid>>("Category").Contains(taxonid))
.Get();
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();
.Where(ci => ci.GetValue<
IList
<Guid>>("Tag").Contains(mtbCategory))
System.ArgumentOutOfRangeException: Database mapped field does not exist.
Parameter name: methodCallExpression
Actual value was ci.FieldValue("Tag").
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 >>
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;
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 >>