Read and edit category information

Namespace:  EPiServer.DataAbstraction
Assembly:  EPiServer (in EPiServer.dll) Version: 5.2.375.236

Syntax

C#
[SerializableAttribute]
public class Category : ICloneable, IListSource

Remarks

Categories are hierarchical data so every Category class has a collection of child categories. There are support functions to help finding child categories both by name or by ID on all categories, see examples below.

Category implements IListSource which mean you can take any category class and databind it to enumerate all child categories in a recursive manner using for example datagrid filtering.

Examples

Example that demonstrates searching for a category with name "Data" and then demonstrates that you can search for a category in another category's children. The Find method always searches recursivly in all children.
CopyC#
Category dataCat = Category.Find("Data");
if (dataCat != null)
{
    Response.Write("Data category was found and had ID:" + dataCat.ID.ToString());
    Category pcCat = dataCat.FindChild("PC");
    if (pcCat != null)
        Response.Write("Data has a child category PC");
}
Example that demonstrates how to create a new category and then deleting a category.
CopyC#
Category root = Category.GetRoot();
Category newCat = new Category("NewCategory", "My new test category");
root.Categories.Add(newCat);
newCat.Save();

Category createdCat = Category.Find("NewCategory");
createdCat.Delete();
Iterate all categories
CopyC#
// Uses EPiServer.DataAbstraction
private void Page_Load(object sender, System.EventArgs e)
{
    // Iterate through all categories
    Category rootCat = Category.GetRoot();
    DebugDump(rootCat, 0);
}

private void DebugDump(Category cat, int Indent)
{
    for (int i = 0; i < Indent; i++)
        Response.Write("&nbsp;");
    Response.Write(string.Format("[{0}] {1} ({2})", cat.ID, cat.Name, cat.Description));
    foreach (Category subCat in cat.Categories)
        DebugDump(subCat, Indent + 2);
}

Inheritance Hierarchy

System..::.Object
  EPiServer.DataAbstraction..::.Category

See Also