Sitefinity CMS

Finding Categories Send comments on this topic.
Developing with Sitefinity > Modules > Modules API > Generic Content > Categories > Finding Categories

Glossary Item Box

There are several ways to retrieve one or more categories. Choose an appropriate function given the particular scenario:

  • GetCategory(string categoryName) - Get a specific Category by its Name
  • GetCategory(Guid categoryID) - Get a specific Category by its ID
  • GetCategories() – Get all Categories
  • GetCategories(string owner) - Get all Categories for given owner
  • GetCategories(int from, int max, string sortExpr) - Get categories from given position and specified number, sorted
  • GetCategories(bool calculateContentCount) - Get categories. If calculateContentCount is true, will also calculate number of content items in each category and assign it to the category's property ContentCount. If false, the property will be 0 
  • GetCategories(int from, int to, string sortExpression, bool calculateContentCount) - Get categories from given position and specified number, sorted. If calculateContentCount is true, will also calculate number of content items in each category and assign it to the category's property ContentCount. If false, the property will be 0
  • CategoriesCount() - Get number of categories

Get a specific Category by its Name:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get a specific category by name
Telerik.Cms.Engine.ICategory secondCategory = contentManager.GetCategory("Category 2");
Response.Write(secondCategory.CategoryName +
"<br />");

 

Get a specific Category by its ID:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get a specific category from the list. This is redundant - just for demonstration
Telerik.Cms.Engine.ICategory secondCategory = contentManager.GetCategory("Category 2");
// get the category by passing its ID
Telerik.Cms.Engine.ICategory categoryItem = contentManager.GetCategory(secondCategory.ID);
Response.Write(categoryItem.CategoryName +
"<br />");

 

Get all Categories :

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// create a list of all categories
IList listOfCategories = contentManager.GetCategories();
if (listOfCategories.Count > 0)
{
 
foreach(Telerik.Cms.Engine.ICategory catItem in listOfCategories)
    Response.Write(catItem.CategoryName +
"<br />");
}

 

Get all Categories for given owner:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// create a list of all categories
IList listOfCategories = contentManager.GetCategories();
if (listOfCategories.Count > 0)
{
 
foreach(Telerik.Cms.Engine.ICategory catItem in listOfCategories)
    Response.Write(catItem.CategoryName +
"<br />");
}

 

Get categories from given position and specified number, sorted:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// create a list of categories, from first ('0'), four by number ('4'), sorted by name
IList listOfCategories = contentManager.GetCategories(0,4,"CategoryName ASC");
if (listOfCategories.Count > 0)
{
   
foreach (Telerik.Cms.Engine.ICategory catItem in listOfCategories)
       Response.Write(catItem.CategoryName +
"<br />");
}

 

Get categories with specified value whether to count content items for categories or not:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// create a list of categories, and true value for calculateContentCount parameter
// which means that the ContentCount property of each category item will contain
// the number of content items that belong to this category
IList listOfCategories = contentManager.GetCategories(true);
if (listOfCategories.Count > 0)
{
   
foreach (Telerik.Cms.Engine.ICategory catItem in listOfCategories)
       Response.Write(catItem.CategoryName +
" has " + catItem.ContentCount + " number of content items. <br />");
}

 

Get categories from given position and specified number, sorted, with specified value whether to count content items for categories or not:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// create a list of categories, from first ('0'), four by number ('4'), sorted by name
// and true value for calculateContentCount parameter
// which means that the ContentCount property of each category item will contain
// the number of content items that belong to this category
IList listOfCategories = contentManager.GetCategories(0, 4, "CategoryName ASC", true);
if (listOfCategories.Count > 0)
{
   
foreach (Telerik.Cms.Engine.ICategory catItem in listOfCategories)
       Response.Write(catItem.CategoryName +
" has " + catItem.ContentCount + " number of content items. <br />");
}

  

Get number of categories:

Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get number of categories
int catCount = contentManager.CategoriesCount();
Response.Write(
"The number of categories is " + catCount + "<br />");