How to programmatically access custom fields and categories for news items
Telerik Support,
I am trying to read published news items by following the steps on this page. I have my list of news items but cannot find any information on how to obtain the following data:
1. Filter the list by category
2. Display data in custom fields that are part of the news items
I don't see "category" or my custom fields returned as part of my news item list. Where is this data kept and how do I programmatically access it? I need to filter the list by category and display data from the custom fields.
Thank you,
Mark
Hello Mark,
1. To filter your list of news items by category you can get the category and compare it to the value of each news item's category field, to see whether this particular category is set to the item. Take a look at the sample below:
NewsManager newsManager = NewsManager.GetManager();
var news = newsManager.GetNewsItems().Where(newsItem => newsItem.Status == ContentLifecycleStatus.Live).ToList();
var tMan =Telerik.Sitefinity.Taxonomies.TaxonomyManager.GetManager();
var myCat = tMan.GetTaxa<HierarchicalTaxon>().Where(t => t.Name ==
"myCategory"
).SingleOrDefault().Id;
var myList =
new
List<Telerik.Sitefinity.News.Model.NewsItem>();
for
(var i = 0; i < news.Count; i++)
var category = news[i].GetValue<TrackedList<Guid>>(
"Category"
);
if
(category.Contains(myCat))
myList.Add(news[i]);