Categories

List of examples:

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
CategoryInfoProvider.SetCategoryInfo(category);

> Back to list of examples

Updating a page category

// Gets the page category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Carnivores", SiteContext.CurrentSiteName);

// 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
	CategoryInfoProvider.SetCategoryInfo(category);
}

> Back to list of examples

Updating multiple page categories

// Gets all first level categories on the current site
var categories = CategoryInfoProvider.GetCategories().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
	CategoryInfoProvider.SetCategoryInfo(category);
}

> Back to list of examples

Creating subcategories

// Gets the parent category
CategoryInfo parentCategory = CategoryInfoProvider.GetCategoryInfo("carnivores", SiteContext.CurrentSiteName);

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
	CategoryInfoProvider.SetCategoryInfo(subcategory);
}

> Back to list of examples

Adding pages to categories

// Gets the category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Dogs", SiteContext.CurrentSiteName);

if (category != null)
{
	// Creates a new instance of the tree provider, needed for page operations
	TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

	// Gets a page
	TreeNode root = tree.SelectNodes()
		.Path("/")
		.OnCurrentSite()
		.CombineWithDefaultCulture()
		.FirstObject;

	// Adds the page to the category
	DocumentCategoryInfoProvider.AddDocumentToCategory(root.DocumentID, category.CategoryID);
}

> Back to list of examples

Removing pages from categories

// Gets the category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Dogs", SiteContext.CurrentSiteName);

if (category != null)
{
	// Creates a new instance of the tree provider, needed for page operations
	TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

	// Gets the page
	TreeNode root = tree.SelectNodes()
		.Path("/")
		.OnCurrentSite()
		.CombineWithDefaultCulture()
		.FirstObject;

	// Gets the page category relationship record
	DocumentCategoryInfo documentCategory = DocumentCategoryInfoProvider.GetDocumentCategoryInfo(root.DocumentID, category.CategoryID);

	if (documentCategory != null)
	{
		// Removes the relationship record from the database
		DocumentCategoryInfoProvider.DeleteDocumentCategoryInfo(documentCategory);
	}
}

> Back to list of examples

Deleting page categories

// Gets the page category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Carnivores", SiteContext.CurrentSiteName);

if (category != null)
{
	// Deletes the category
	CategoryInfoProvider.DeleteCategoryInfo(category);
}

> Back to list of examples