More in this section
Forums / General Discussions / How to get Category or filter using Category of News Item programmatically C#?

How to get Category or filter using Category of News Item programmatically C#?

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
2 posts, 0 answered
    RIYAJUDEEN avatar
    2 posts
    01 Aug 2012
    07 Dec 2012
    Link to this post

    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, 


    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;

  2. Victor Velev
    Victor Velev avatar
    1345 posts
    06 Jul 2015
    11 Dec 2012
    Link to this post

    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)
              // 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();

    and the list of used namespaces:

    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;

    The GetValue method is part of the Telerik.Sitefinity.Model namespace.

    You can further extend the sample to suit it to your needs.

    Victor Velev
    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