How to get Category or filter using Category of News Item programmatically C#?
Hi,
I am using Following coding for get News Items, It's working fine.But I want filter the news items by Categories.
My Categories are,
Categories
1. News (Parent Category)
i) Singapore (Child Category)
ii) Internationl (Child Category)
2. News & Events,Errta (Parent Category)
i) Singapore (Child Category)
ii) Internationl (Child Category)
In above, I want fillter the news items for two types,
Type !: Fillter News Item which is under the News & Events,Errta (Parent Category) & Singapore (Child Category)
Type 2: Fillter News Item which is under the News & Events,Errta (Parent Category) & Internationl (Child Category)
In above operation how can I perform using code behind?
My NewsItems Collect Function...
var newsitems = App.WorkWith().NewsItems().Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Get().ToList();
foreach
(var item
in
newsitems)
string
Title =
string
.Empty;
string
Summary =
string
.Empty;
DateTime dtPub;
Title = item.Title.ToString();
Summary = item.Summary.ToString();
dtPub = item.PublicationDate;
Hello,
If you want to programmatically filter news items based on categories, you can check the following code sample which will get all news items based on a category of your choosing.
public
Taxon GetCategoryByTitle(
string
title)
var taxonomyMgr = TaxonomyManager.GetManager();
HierarchicalTaxonomy category = taxonomyMgr.GetTaxonomies<HierarchicalTaxonomy>().Where(x => x.Name ==
"Categories"
).SingleOrDefault();
if
(category ==
null
)
return
null
;
return
category.Taxa.Where(x => x.Title == title).SingleOrDefault();
public
List<NewsItem> GetContentByCategory(Guid categoryId)
Taxon catTaxon = GetCategoryByTitle(
"TestCategory1"
);
List<NewsItem> newsItemsList = GetContentByCategory(catTaxon.Id);
var taxonomyMgr = TaxonomyManager.GetManager();
var newsMgr = NewsManager.GetManager();
// Get the Id of the category
var taxonId = taxonomyMgr.GetTaxa<HierarchicalTaxon>()
.Where(t => t.Id == categoryId)
.SingleOrDefault()
.Id;
// Get all news items that are assigned to this category
var NewsItemsInCategory = newsMgr.GetNewsItems().Where(p => p.Status == ContentLifecycleStatus.Live && p.GetValue<TrackedList<Guid>>(
"Category"
).Contains(taxonId));
return
NewsItemsInCategory.ToList();
using
Telerik.Sitefinity.GenericContent.Model;
using
Telerik.Sitefinity.Modules.News;
using
Telerik.Sitefinity.News.Model;
using
Telerik.Sitefinity.Taxonomies;
using
Telerik.Sitefinity.Taxonomies.Model;
using
Telerik.Sitefinity.Model;
using
Telerik.OpenAccess;