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

Forums / Developing with Sitefinity / Get All Generic Content Items by Category ID

Get All Generic Content Items by Category ID

5 posts, 0 answered
  1. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    09 Oct 2009
    Link to this post
    I've been working on this one now for a couple of days now so I decided to head strait to the source.  I've seen a number of examples none of which address my needs exactly.  Here is my case scenario:  I am creating a user control that will display a random item from a list of items under a specific Category.  I've already created the Generic Content Items and specified a specific Category for them, for the purpose of this explanation we'll call the Category 'Sports' with a CategoryID of VVVVVVVV-WWWW-XXXX-YYYY-ZZZZZZZZZZZZ.)  I am not asking how to get an item randomly but rather how to get all items (Generic Content) that is assigned to a Category.  I'd like to get the list by CategoryID (Guid).  Do you have any examples that I could use to get me back into Development mode?  Thanks a lot for your help!
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Oct 2009
    Link to this post
    Hello Scott Harrington,

    I am not asking how to get an item randomly but rather how to get all items (Generic Content) that is assigned to a Category.  I'd like to get the list by CategoryID (Guid)

    To get a IList of content items that are associated with a category you can use ContentFilterBuilder. Below is a sample code:

    ContentManager cntManager = new ContentManager();
           ContentFilterBuilder filter = new ContentFilterBuilder(this);
           //GET A LIST OF ALL CATEGORIES FOR OUR PROVIDER
           //YOU CAN USE ONLY STRING IN THE FILTER - ONE CATEGORY
           //OR ADD SEVERAL FILTER STATEMENTS
           IList listOfCategories = cntManager.GetCategories();
           //WE ARE GETTING A SINGLE CATEGORY WHICH NAME WILL PASS TO THE FILTER
           Telerik.Cms.Engine.ICategory cat = cntManager.GetCategory(new Guid("A8A8A6BC-EA45-4b77-8495-C958D817D311"));
           string categoryName = cat.CategoryName;
           // APPLY THE FILTER TO OUR CONTROL (this)
           filter.AddFilter(new ContentFilterStatement("Category", categoryName,
                                                                            ContentFilter.Condition.In,
                                                                            ContentFilter.JoinType.And));

    GetCategories method does not expose overload that you can use array of guids as parameter. The same applies for GetCategory method.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 Oct 2009
    Link to this post
    Thanks for the great example.  I do however have an additional question spurred by your example.  I see that you used 'this' as a parameter for:

      ContentFilterBuilder filter = new ContentFilterBuilder(this);

    This tells me that the class needs to inherit from the interface 'IFilterableContentControl' class implementing the property 'public string FilterExpression'.  NOTE: that this filter is on a UserControl that currently doesn't implement any interfaces.  I am not exactly sure what my filter Expression should be?  I figured my filter would be built in the last line of your example:

    filter.AddFilter(new ContentFilterStatement("Category", categoryName,
                                                                            ContentFilter.Condition.In,
                                                                            ContentFilter.JoinType.And));

    But if I leave the FilterExpression blank - Return ""; - the page fails to load.  Do you have any suggestions to what I should put in the FilterExpression property?  Remember, I am only looking at getting all Generic Content Items for a given Category.  Thanks a lot for your help and your quick responses - Great customer support!
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Oct 2009
    Link to this post
    Hi Scott Harrington,

    You can replace "this" with a control that implements IFilterableContentControl - these are all controls that inherits from ContentView class. Another option is using MetaSearchInfo filter.

    sample code:

    Telerik.Cms.Engine.ContentManager contentManager =  new Telerik.Cms.Engine.ContentManager();
    List<Telerik.Cms.Engine.IMetaSearchInfo> filter = new List<Telerik.Cms.Engine.IMetaSearchInfo>();
    filter.Add(new Telerik.Cms.Engine.MetaSearchInfo(Telerik.Cms.Engine.MetaValueTypes.ShortText, "Category", "CategoryName"));
    IList listOfContentItems = contentManager.GetContent(filter.ToArray());
    if (listOfContentItems.Count > 0)
    {
       foreach (Telerik.Cms.Engine.IContent contentItem in listOfContentItems)
           //DO SOMETHING HERE
    }

    I hope this helps.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 Oct 2009
    Link to this post
    That one did it!  Exactly what I was looking for.  As always thanks for your help and quick responses.
Register for webinar
5 posts, 0 answered