31.10.2014 Views

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16 | Developer’s <strong>Guide</strong> <strong>EPiServer</strong> Community 3.1<br />

2.4 Categories<br />

The Category system allows for categorization of objects that implements the IcategorizableEntity interface. In this<br />

tutorial we use a Blog as an example.<br />

The use the Category system, first import the necessary namespaces:<br />

using StarCommunity.Core.Modules;<br />

using StarCommunity.Core.Modules.Security;<br />

using StarCommunity.Core.Modules.Categories;<br />

using StarCommunity.Modules.Blog; //only for this example.<br />

2.4.1 Add a category<br />

Categories may be added programmatically or in the administration interface. Categories a stored in a tree structure as<br />

shown in the example below.<br />

//Create a new (root) category<br />

ICategory category = new Category("cars");<br />

//Commit to database<br />

ICategory rootCategory = CategoryHandler.AddCategory(category);<br />

//Create a new sub category to root category<br />

ICategory subCategory = new Category("Volvo", rootCategory);<br />

//Commit to database<br />

CategoryHandler.AddCategory(subCategory);<br />

2.4.2 Remove a category<br />

Categories may be removed programmatically or in the administration interface. You can get a category by id, by path<br />

or by name. This is an example to remove a category by its path.<br />

//Get the category to remove by path<br />

ICategory category = CategoryHandler.GetCategory("cars/Volvo");<br />

//Remove the category<br />

CategoryHandler.Removecategory(category);<br />

2.4.3 Categorize an entity<br />

A categorizable entity can have one or more categories added to it.<br />

//Get the blog to categorize by id<br />

Blog blog = (Blog)BlogHandler.GetBlog(1).Clone();<br />

© <strong>EPiServer</strong> AB

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!