AI OnAI Off
Hello Hans
It sounds like you should use an EPiServer Initialisation module for this:
It might also be worth checking for the existence of the category before inserting it to avoid any issues.
David
I'm using following fragment to create new categories from code (keep in mind that code will execute *every* time app pool will start - so you should take care of avoiding multiple creation of the same category):
namespace MyProject.Web.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class CategorySetup : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
CreateIfNotExists("Key", "Display Name", 1);
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
private void CreateIfNotExists(string name, string description, int order)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (string.IsNullOrEmpty(description))
{
throw new ArgumentNullException("description");
}
var categories = CategoryList.LoadCategories();
if (categories.Contains(name))
{
return;
}
var category = new Category(name, description)
{
Available = true,
Parent = Category.GetRoot(),
SortOrder = order
};
category.Save();
}
}
}
Hi,
Is there a way to dynamically create categories from my c# code which I can use in the categories field in EPiServer?
I have found a way to do this:
However, this should be done once on EPiServer initialization. Should I create a plugin or use a descriptor for it?
Hope somebody can help!