+1-888-365-2779
Try Now
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#?

2 posts, 0 answered
  1. RIYAJUDEEN
    RIYAJUDEEN avatar
    2 posts
    Registered:
    01 Aug 2012
    07 Dec 2012
    Link to this post
    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;
    }

  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    11 Dec 2012
    Link to this post
    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();
          }

    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.

    Greetings,
    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