More in this section

Forums / Developing with Sitefinity / News GetContent Filter using Tags and Categories

News GetContent Filter using Tags and Categories

4 posts, 0 answered
  1. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    22 Feb 2010
    Link to this post
    I am trying to create a user control that is tied to the News api. This control needs to filter by both category and tags. It should display the article title and a read more link. I am able to filter by category but the tag filter is not working. Also, how do I get the url for the read more link? I have a newsView on a separate page that the control will link to.

    protected void Page_Load(object sender, EventArgs e) {
    // create new instance of NewsManager
    Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News");
    List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>();
    filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", _newsCategory, SearchCondition.Equal, JoinType.And));
    filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Tag.Name", _newsTag, SearchCondition.Equal, JoinType.And));
    //IList items = newsManager.Content.GetContent(filter.ToArray());
    // get all news content items
    IList listOfNewsItems = newsManager.Content.GetContent(0, _newsMaxItems, "Publication_Date DESC", filter.ToArray());
    if (listOfNewsItems.Count > 0) {
    foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems)
    //Response.Write(newsItem.GetMetaData("Title") + "<br />");
    bodyNews.Controls.Add(new LiteralControl("<p>" + newsItem.GetMetaData("Title") 
    + "&nbsp<a href=\"" + newsItem.GetMetaData("Source") + "\">READ MORE</a></p>"));
    }
    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Feb 2010
    Link to this post
    Hello Connections Academy Developer,

    Please take a look at this post that shows how to filter by Category and tag Events View DisplayMode.
    Filtering for all content items is the same so it does not matter that I am showing how to do this with Events.

    Also you can use ContentFilterBuilder class instead IMetaSearchInfo filter.

    ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
    filterBuilder.AddFilter(new ContentFilterStatement("Tag.Name", tag.TagName,ContentFilter.Condition.Equal, ContentFilter.JoinType.And));
    filterBuilder.AddFilter(new ContentFilterStatement("Category", categoryNames,ContentFilter.Condition.In, ContentFilter.JoinType.And));

    Read more link is a Hyperlink control with ID="fullContent1". The control is located inside a Repeater with ID="repeater". You can get the fullContent1 hyperlink by subscribing for ItemDataBound event of the repeater and then call e.FindControl and pas the ID of the Hyperlink/

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    22 Feb 2010
    Link to this post
    Thank you, I was able to get the category and tag filter to work by modifying the code a bit:

    protected void Page_Load(object sender, EventArgs e) {
            Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News");
                //add filter
                List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>();
                filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", _newsCategory, SearchCondition.Equal, JoinType.And));
                IList listOfNewsItems = newsManager.Content.GetContent(0, _newsMaxItems, "Publication_Date DESC", _newsTag, null, string.Empty, null, filter.ToArray());
                 
                if (listOfNewsItems.Count > 0) {
                    foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems)
                        bodyNews.Controls.Add(new LiteralControl("<p>" + newsItem.GetMetaData("Title")
                            + " <a href=\"" + newsItem.GetMetaData("Source") + "\">READ MORE</a></p>"));
                }
                 
            }

    Is there a way to get the Read More/Full Story link using the news api? I do not have a newsView control on this page, I am creating a simple user control with just the news item title and link.

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Feb 2010
    Link to this post
    Hello Connections Academy Developer,

    Using ContentView controls as user controls is not considered as a good practice. However the full Story link can be accessed from the template - ListPageMaster using FindControl method of the RepeaterItemEventArgs.
    Another option is creating a custom control that inherits from NewsView and accessing the fullStory link by overriding SetItemContent

    for (int i = 1; i < 6; i++)
    {
        ctrl = FindContentViewControl("fullContent" + i, itemContainer);
        if (ctrl is HyperLink)
        {
              // do your job here
        }
     }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
4 posts, 0 answered