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

Forums / Developing with Sitefinity / Getting Library Items in a Given Category

Getting Library Items in a Given Category

6 posts, 1 answered
  1. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    20 Aug 2009
    Link to this post
    Hello,

    I have been using the API extensively with a custom control for one of our client's websites. The purpose of this control is to mimic a filesystem to access the contents of libraries.

    The client requested nested folders, and believing that the Sitefinity Images & Documents module does not support nesting libraries, I began using categories, which can be nested, to create this architecture. Currently, the control correctly displays the bottom-level categories, and then clicking each category will display its subcategories. Where I am having trouble is returning a list of library items belonging to a given category. Is there a method somewhere that I am missing which works in the following manner?

    LibraryManager lm = new LibraryManager(); 
    ICategory category = lm.GetCategory(catID); 
     
    IList items = category.GetItems(); 

    I know that it is possible to get the items of a library using the getItems method of the ILibrary class, so it seems that you should be able to get the items within a given category as well.

    Thank you for your support,
    David
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    21 Aug 2009
    Link to this post
    Hello David,

    It is possible to return the items form a given category. The way you used in your sample is more complicated because you cannot pass category ID - a Guid in GetCotentent(). You can use IMetaSearchInfo filter.  Sample code can be found below:

            LibraryManager lManager = new LibraryManager(); 
            ICategory cat = lManager.GetCategory(new Guid("72AD1DDE-2CBC-4E4C-8F8E-5A922BC46882")); 
            IMetaSearchInfo filter = new MetaSearchInfo(MetaValueTypes.ShortText, "Category", cat.CategoryName); 
            IList listofItems = lManager.GetContent(filter); 
            foreach (IContent cont in listofItems) 
            { 
                Response.Write(cont.GetMetaData("Name").ToString()); 
            } 

    If you are using a custom control that inherits from a ContentView control you can add the following logic.

        protected override System.Collections.IList CreateDataSource() 
        { 
            Filter(); 
            return base.CreateDataSource(); 
           
        } 
        void Filter() 
        { 
            Telerik.Cms.Engine.ICategory cat = this.manager.GetCategory(new Guid("72AD1DDE-2CBC-4E4C-8F8E-5A922BC46882")); 
            ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this); 
            ContentFilterStatement categoryFilter = 
                                        new ContentFilterStatement("Category", cat.CategoryName, 
                                            ContentFilter.Condition.In); 
            filterBuilder.AddFilter(categoryFilter); 
     
        } 


    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.
    Answered
  3. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    21 Aug 2009
    Link to this post
    Fantastic!

    Once again, thanks for your wonderful support.

    David
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    23 Aug 2009
    Link to this post
    Hello David,

    Always glad to help.

    All the best,
    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. clayman
    clayman avatar
    38 posts
    Registered:
    23 Sep 2009
    19 Oct 2009
    Link to this post
    What if I need to structure my content categories in a hierarchy - for example:

                English 
                   News 
                   Press Release 
                French 
                   News 
                   Press Release 

    If I want to filter on English/News (and not French/News), how can I accomplish that using the API?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    19 Oct 2009
    Link to this post
    Hello clayman,

    ICategory object has ParentCategoryID property that you can use. As I see it in your case English will be the parent category of News and PressReleases.

    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.
Register for webinar
6 posts, 1 answered