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

Forums / Developing with Sitefinity / Get tags for certain category of news module

Get tags for certain category of news module

5 posts, 0 answered
  1. Yordan
    Yordan avatar
    6 posts
    Registered:
    13 Feb 2009
    19 Feb 2009
    Link to this post
    Hello,

    I would like to ask a question related to News module. Is there a way to get all tags, used in the news items of certain category? What I tried was to use the ContentManager GetTags with the id of the category but it does not work ...
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    19 Feb 2009
    Link to this post
    Hello Yordan,

    Here is how to get all tags from the items added to a certain category:

           // set the name of the category 
            string category = newsCategory; 
            // new instance of NewsManager 
            NewsManager newsManager = new NewsManager("News"); 
            // GetCategory 
            ICategory mycategory = newsManager.Content.GetCategory(category); 
            List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
            filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", category, SearchCondition.Equal, JoinType.And)); 
            IList items = newsManager.Content.GetContent(filter.ToArray()); 
     
            IList tags = newsManager.Content.GetTags(); 
            IList<ITag> usedTags = new List<ITag>(); 
     
            foreach (ITag tag in tags) 
            { 
                foreach (ITaggedContent taggedCnt in tag.TaggedContent) 
                { 
                    foreach (IContent item in items) 
                    { 
                        // use intersection here to compare the items 
                        if (item.ID == taggedCnt.ContentID) 
                        { 
                            usedTags.Add(tag); 
                            break
                        } 
                    } 
                } 
            } 
            foreach (ITag tag1 in usedTags) 
            { 
                Response.Write(tag1.TagName); 
            } 
     
        // here add property of the control where we set the category from UI 
        public string NewsCategory 
        { 
            get { return this.newsCategory; } 
            set { this.newsCategory = value; } 
        } 
        private string newsCategory; 
     


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Yordan
    Yordan avatar
    6 posts
    Registered:
    13 Feb 2009
    20 Feb 2009
    Link to this post
    Thanks. After some modifications everythings works OK. I have another question and I will ask here:

    I have an IList with some IContent objects, which I want to bind to the CustomDataSource property of a wrapped NewsView contol like this:

     

        protected override void Page_Load(object sender, EventArgs e)  
        {  
            NewsManager myNewsManager = new NewsManager("News");  
     
            this.NewsView.CustomDataSource = myNewsManager.Content.GetContent(0, 0, "Publication_Date DESC""BQ News1");//GetFilteredContent();  
            this.NewsView.DataBind();  
             
            base.Page_Load(sender, e);  
        } 

    The GetContent method returns some items, but the NewsView still displays all news. Any idea why?

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Feb 2009
    Link to this post
    Hi Yordan,

    Using CustomDataSource you cannot filter, sort and so on the items. You use the NewsView control only to show the data from your source and it is displayed in the same way as it is in the source. The NewsView "becomes" as the Repeater control.

    NewsManager newsManager = new NewsManager("News"); 
    NewsView1.CustomDataSource = newsManager.Content.GetContent(0, 1); 

    I hope this helps.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    01 Feb 2011
    Link to this post
    Hi, I know this is an old thread, but I came across it as I was searching for a way to do the same thing.
    I believe I have come up with a faster way to accomplish this using two nested loops instead of three:

    ContentManager cm = new ContentManager("News");
     
    foreach (ITag tag in Tags)
    {
         foreach (ITaggedContent c in tag.TaggedContent)
           {
    if(cm.GetContent(c.ContentID).GetMetaData("Category").ToString().Equals(CategoryName))
    {
       usedTags.Add(tag);
       break;
    }
           }
    }

    This should get the job done in two loops instead of 3.
Register for webinar
5 posts, 0 answered