Categories
List of examples:
- Creating a new page category
- Updating a page category
- Updating multiple page categories
- Creating subcategories
- Adding pages to categories
- Removing pages from categories
- Deleting page categories
Creating a new page category
// Creates a new category object
CategoryInfo category = new CategoryInfo();
// Sets the category properties
category.CategoryDisplayName = "Carnivores";
category.CategoryName = "Carnivores";
category.CategoryDescription = "Animals that feed on other animals";
category.CategorySiteID = SiteContext.CurrentSiteID;
category.CategoryEnabled = true;
// Saves the category to the database
CategoryInfo.Provider.Set(category);
Updating a page category
// Gets the page category
CategoryInfo category = CategoryInfo.Provider.Get("Carnivores", SiteContext.CurrentSiteID);
// Checks if the previous method retrieved the record
if (category != null)
{
// Updates the category properties
category.CategoryDescription = "People who are not vegetarians";
// Saves the changes to the database
CategoryInfo.Provider.Set(category);
}
Updating multiple page categories
// Gets all first level categories on the current site
var categories = CategoryInfo.Provider.Get().WhereEquals("CategoryLevel", 0).OnSite(SiteContext.CurrentSiteID);
// Loops through the categories
foreach (CategoryInfo category in categories)
{
// Updates the code names of the categories
category.CategoryName = category.CategoryName.ToLower();
// Saves the changes to the database
CategoryInfo.Provider.Set(category);
}
Creating subcategories
// Gets the parent category
CategoryInfo parentCategory = CategoryInfo.Provider.Get("carnivores", SiteContext.CurrentSiteID);
if (parentCategory != null)
{
// Creates a new category object
CategoryInfo subcategory = new CategoryInfo();
// Sets basic properties
subcategory.CategoryDisplayName = "Dogs";
subcategory.CategoryName = "Dogs";
subcategory.CategoryDescription = "Man's best friends";
subcategory.CategorySiteID = SiteContext.CurrentSiteID;
subcategory.CategoryEnabled = true;
// Assigns to the parent category
subcategory.CategoryParentID = parentCategory.CategoryID;
// Saves the category to the database
CategoryInfo.Provider.Set(subcategory);
}
Adding pages to categories
// Gets a page category from the system
CategoryInfo category = CategoryInfo.Provider.Get("Dogs", SiteContext.CurrentSiteID);
if (category != null)
{
// Gets a page
TreeNode page = new DocumentQuery<TreeNode>()
.Path("/Example", PathTypeEnum.Single)
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
// Adds the page to the category
DocumentCategoryInfo.Provider.Add(page.DocumentID, category.CategoryID);
}
Removing pages from categories
// Gets a page category from the system
CategoryInfo category = CategoryInfo.Provider.Get("Dogs", SiteContext.CurrentSiteID);
if (category != null)
{
// Gets a page
TreeNode page = new DocumentQuery<TreeNode>()
.Path("/Example", PathTypeEnum.Single)
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
// Gets the page category relationship record
DocumentCategoryInfo documentCategory = DocumentCategoryInfo.Provider.Get(page.DocumentID, category.CategoryID);
if (documentCategory != null)
{
// Removes the relationship record from the database
DocumentCategoryInfo.Provider.Delete(documentCategory);
}
}
Deleting page categories
// Gets the page category
CategoryInfo category = CategoryInfo.Provider.Get("Carnivores", SiteContext.CurrentSiteID);
if (category != null)
{
// Deletes the category
CategoryInfo.Provider.Delete(category);
}