Sitefinity CMS

Modifying Categories Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Generic Content > Categories > Modifying Categories

Glossary Item Box

Updating a Category is a very simple process:

  1. Get a Category associated with a transaction
  2. Change its properties
  3. Save the Category with ContentManager

 

There are three methods for updating a category:

  • SaveCategory(ICategory category) - Save a specified category item
  • UpdateContentCategory(string oldCategoryName, string newCategoryName) - Update the name of a category item in all its content items by passing the old and the new name
  • UpdateCategory(Guid ID, string CategoryName) - Update name of category with specified ID

Save a specified category item:

SaveCategory(ICategory category) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all categories
IList listOfCategories = contentManager.GetCategories();
if (listOfCategories.Count > 0)
{
   
// gte the third category
   
Telerik.Cms.Engine.ICategory thirdCategory = contentManager.GetCategory(((Telerik.Cms.Engine.ICategory)listOfCategories[2]).ID);
   
// save new value of the CategoryName property
   
thirdCategory.CategoryName = "Changed Name of Category 3";
   
// save the category object to database
   
contentManager.SaveCategory(thirdCategory);
   Response.Write(thirdCategory.CategoryName +
"<br />");
}

 

Update the name of a category item by passing the old and the new name:

The name of the category itself will not change - rather all content items that belong to this category will have a changed category name. This will also make the current category with zero content items.
UpdateContentCategory(string oldCategoryName, string newCategoryName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// update category name in all content that belongs to this category
contentManager.UpdateContentCategory("Category 5", "New Category 5");

 

Update name of category with specified ID:

UpdateCategory(Guid ID, string CategoryName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all categories
IList listOfCategories = contentManager.GetCategories();
if (listOfCategories.Count > 0)
{
   
// get the fourth category
   
Telerik.Cms.Engine.ICategory fourthCategory = contentManager.GetCategory(((Telerik.Cms.Engine.ICategory)listOfCategories[4]).ID);
   
// update the category with passed ID and assign new name
   
contentManager.UpdateCategory(fourthCategory.ID,"Updated name 2");
   Response.Write(fourthCategory.CategoryName +
"<br />");
}

 

See Also