How to programmatically access custom fields and categories

Posted by Community Admin on 04-Aug-2018 01:07

How to programmatically access custom fields and categories for news items

All Replies

Posted by Community Admin on 14-Feb-2012 00:00

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

Posted by Community Admin on 16-Feb-2012 00:00

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]);
                
                 
            
2. Getting the value of a custom field can be done with the GetValue method. Take a look at this forum thread for further reference.

Regards,
Jen Peleva
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

This thread is closed