API
Version 7.x > API > How to programatically create Categories View modes: 
User avatar
Member
Member
hemanthray-gmail - 6/10/2013 11:35:22 AM
   
How to programatically create Categories
Do we have to just update the CMS_category table to add new categories ? Or is there any way we could add categories via API into kentico ?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 6/10/2013 12:16:03 PM
   
RE:How to programatically create Categories
In the API Documentation it shows there is a CategoryInfoProvider class which would allow you to create a new CategoryInfo() object. I'd assume the code would look something like this:
CategoryInfo ci = new CategoryInfo();
ci.CategoryName = "Cat 1";
ci.CategoryDisplayName = "Category 1";

CategoryInfoProvider.SetCategoryInfo(ci);

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 6/11/2013 3:45:08 AM
   
RE:How to programatically create Categories
Hello,

I would also recommend you to take a look at the CMS API Examples (CMSSiteManager -> Support -> Api Examples -> Documents -> Categories) where you can find also the example code to create a categori:
private bool CreateCategory()
{
// Create new category object
CategoryInfo newCategory = new CategoryInfo();

// Set the properties
newCategory.CategoryDisplayName = "My new category";
newCategory.CategoryName = "MyNewCategory";
newCategory.CategoryDescription = "My new category description";
newCategory.CategorySiteID = CMSContext.CurrentSiteID;
newCategory.CategoryCount = 0;
newCategory.CategoryEnabled = true;

// Save the category
CategoryInfoProvider.SetCategoryInfo(newCategory);

return true;
}

Brandon's example is exactly the same just a little bit simplified.

Best regards,
Martin Danko