1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to programmatically access custom fields and categories for news items

How to programmatically access custom fields and categories for news items

2 posts, 0 answered
  1. Mark
    Mark avatar
    22 posts
    Registered:
    16 Sep 2012
    14 Feb 2012
    Link to this post
    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
  2. Jen Peleva
    Jen Peleva avatar
    568 posts
    Registered:
    10 Nov 2016
    16 Feb 2012
    Link to this post
    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
2 posts, 0 answered