Working with rules and categories for Extension functions

by: Fredrik Karlsson

I have just added two more articles on the Extension wiki and I thougt I might post the code and a short description here as well.


Rules are used in Extension to set whether or not you can add function in a Content area or not. Because if you have a small content area to the right that is used for ads and other small functions, you might not want the editorial people to add a XForm function there. Heres the code to add a rule in codebehind.

// Load all rules...
Dropit.Extension.Core.RuleContentAreaCollection rcac = Dropit.Extension.Controllers.RuleContentAreaManager.XMLRuleContentAreaList();

// Load our function, "FiftyFifty". 
//Each funciton is an EPiServer pagetype so the code is the 
//same if you need to add funcitons to an Extension pagetype 
EPiServer.DataAbstraction.PageType pt = EPiServer.DataAbstraction.PageType.Load("[ExtensionSys] FiftyFifty");
Dropit.Extension.Core.ContentFunctionType cft = Dropit.Extension.Core.ContentFunctionType.Load(pt.ID);

// Load our function "Text"
EPiServer.DataAbstraction.PageType ptText = EPiServer.DataAbstraction.PageType.Load("[ExtensionSys] Text");
Dropit.Extension.Core.ContentFunctionType cftText = Dropit.Extension.Core.ContentFunctionType.Load(ptText.ID);

// Add a rule saying that we can add our function "Text" in the content area LeftArea
rcac.AddRule(cft.FunctionTypeID, "LeftArea", cftText.FunctionTypeID);

// Save the rules...

Read more here


Categories are simply used to split the functions into different categories for the tool box UI. Very useful if you end up with many functions. Heres the code to add a category in code behind.

// Everything in Extension is EPiServer, so lets load the EPiServer pagetype for our function
EPiServer.DataAbstraction.PageType pt = EPiServer.DataAbstraction.PageType.Load("[ExtensionSys] Text");

// Lets load the Extension ContentFunction type
// This is not necessary in this example since they got the same ID, but it's still good to know how it works
Dropit.Extension.Core.ContentFunctionType cft = Dropit.Extension.Core.ContentFunctionType.Load(pt.ID);

// Load our EPiServer Category
EPiServer.DataAbstraction.Category c = EPiServer.DataAbstraction.Category.Find("Text");

// Load our Extension category
// As before, this is not necessary, but its good to know how it works...
Dropit.Extension.Core.ExtensionCategory ec = Dropit.Extension.Controllers.CategoryManager.GetExtensionCategoryByID(c.ID);

// Add the function type to the category
Dropit.Extension.Controllers.CategoryManager.AddFunctionTypeToCategory(cft.FunctionTypeID, ec.ID);
// Alternative way

// Remove the function type from the selected extension category

// Remove the function type from all categories
Read more here

22 September 2008


Post a comment    
User verification Image for user verification  
EPiTrace logger