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

Forums / Developing with Sitefinity / Blog Posts by Category ID and Tag ID

Blog Posts by Category ID and Tag ID

10 posts, 0 answered
  1. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    12 Feb 2009
    Link to this post

    Hello

    I am working with the blog API, and had a question on how I would go about getting blog posts by Category ID and Tag ID.

    I found some examples and I can fetch posts using category names, but i'd prefer to use the id.

    Any help, advice would be greatly appreciated.

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Feb 2009
    Link to this post
    Hi Imran,

    Here is an example that shows how to get blog posts by category ID and tag ID.

      Guid catid = new Guid("361BA51E-132E-4BC5-B3B7-741AD7BC6736"); 
             
     
          // create new instance of BlogManager 
          BlogManager blogManager = new BlogManager(); 
          // get all blogs. This is redundant - just for demonstration 
          IList listOfAllBlogs = blogManager.GetBlogs(); 
          foreach (IBlog blogItem in listOfAllBlogs) 
              if(blogItem.Name == "MyBlog"
                    { 
                        Guid parentId = blogItem.ID; 
                        // add filter 
                        ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this); 
                        filterBuilder.AddFilter(new ContentFilterStatement("Category.ID", catid.ToString(), ContentFilter.Condition.Equal, ContentFilter.JoinType.And)); 
                        filterBuilder.AddFilter(new ContentFilterStatement("ParentID", parentId.ToString(), ContentFilter.Condition.Equal, ContentFilter.JoinType.And)); 
                        IList allposts = blogManager.GetPosts(string.Empty, filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter()); 
                        foreach(IContent post in allposts) 
                           Response.Write(post.Content + "<br />"); 
                    }  

    You can add a new property from where you can set the IDs instead of directly in the code

     private string filterExpression; 
        public string FilterExpression 
        { 
            get 
            { 
                return this.filterExpression; 
            } 
            set 
            { 
                this.filterExpression = value; 
            } 
        } 

    I hope this helps.

    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. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    13 Feb 2009
    Link to this post
    Ivan

    Many thanks for this. Any ideas where can I find some documentation on ContentFilterBuilder?

    Your query below gets posts that belong to a certain blog and category. if i wanted to add publication status to that query, how would i do that? Or do I just just filter the posts in the foreach loop for ContentStatus ?

    Many thanks
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    13 Feb 2009
    Link to this post
    Hi Imran,

    We will try to include some documentation for ContentFilterBuilder class. We will update our documentation next week.

    Here is the way to get posts by theirs ContentStatus

    IList allposts = blogManager.Content.GetContent(0, 0, string.Empty, ContentStatus.Draft, filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter()); 

    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.
  5. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    13 Feb 2009
    Link to this post
    Ivan, thank you very much for your support. I've been looking at the existing documentation for ContentManager.GetContent and its various overloads. I made this statement and it works fine (using category name):

    List

     

    <IMetaSearchInfo> filter = new List<IMetaSearchInfo>();
    filter.Add(
    new MetaSearchInfo(MetaValueTypes.ShortText, "Category", "somename");

     

     

    IList posts = new BlogManager().Content.GetContent(0, 0, "Publication_Date DESC", ContentStatus.Published,filter.ToArray());

    Now if i try to use this, using the category id, it doesnt return anything:

     

    List

     

    <IMetaSearchInfo> filter = new List<IMetaSearchInfo>();
    filter.Add(
    new MetaSearchInfo(MetaValueTypes.Guid, "Category.ID", new Guid("6b4f4058-0a0c-4a4a-bea8-6b228c6b118d")));

     

     

    IList posts = new BlogManager().Content.GetContent(0, 0, "Publication_Date DESC", ContentStatus.Published,filter.ToArray());

    Am i doing something wrong ? Many thanks

     

  6. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    13 Feb 2009
    Link to this post
    Also, I've wrapped the telerik blog objects in my own classes, so how can i use ContentFilterBuilder ?
  7. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    16 Feb 2009
    Link to this post
    Is it possible to use MetaSearchInfo to search for content that belong to a tag?

    Can be MetaSearchInfo used to find content that belongs to a specific category and one or more tags?
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Feb 2009
    Link to this post
    Hi Imran,

    The example below returns content items by tag and category. Please take a look at it.

           string Category = "MyCat";  
           string Tag = "MyTag";  
              
            // new instance of BlogsManager  
            BlogsManager blogsManager = new BlogsManager("Blogs");  
            //add filter  
            List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>();  
            filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", Category, SearchCondition.Equal, JoinType.And)); 
            // get blogs using GetContent  
            IList listOfAllBlogs = blogsManager.Content.GetContent(0, 0, string.Empty, MyTag, nullstring.Empty, null, filter.ToArray());  
            //show the events by title.  
        }  

    I hope this helps.

    Sincerely yours,
    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.
  9. Imran
    Imran avatar
    75 posts
    Registered:
    17 Jan 2007
    17 Feb 2009
    Link to this post
    Ivan

    Thank you for this. I was hoping that you would be able to provide an example that shows searching for content that belongs to two or more tags?

    i.e Find all blog content that is tagged with "mytag1" and "mytag2"

    Thank you
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    17 Feb 2009
    Link to this post
    Hi Imran,

    Posts that have been tagged more than once could be listed using intersection as the example below demonstrates.

            string Tag1 = "MyTag1";   
            string Tag2 = "MyTag2";   
               
            BlogManager blogsManager = new BlogManager("Blogs");   
            IList listOfAllBlogs = blogsManager.Content.GetContent(0, 0, string.Empty, Tag1); 
            IList listOfAllBlogs1 = blogsManager.Content.GetContent(0, 0, string.Empty, Tag2); 
            // here we are using intersection 
            List<IContent> intersection = new List<IContent>(); 
            foreach (IContent c1 in listOfAllBlogs) 
            { 
                foreach (IContent c2 in listOfAllBlogs1) 
                { 
                    if (c1.ID == c2.ID) 
                    { 
                        intersection.Add(c1); 
                        break
                    } 
                    // list the content titles here 
                } 
            } 


    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.
Register for webinar
10 posts, 0 answered