简体   繁体   English

以编程方式检索可以分配给Episerver中的页面的类别

[英]Programmatically retrieve the categories that can be assigned to pages in Episerver

Does anyone have an idea of how to programmatically retrieve the categories that can be assigned to pages in Episerver ? 有没有人知道如何以编程方式检索可以分配给Episerver中的页面的类别 C# is the programming language that I am using but an example in VB will do as well. C#是我正在使用的编程语言,但VB中的一个例子也可以。

If you're after all the categories defined in the CMS, then start with fetching the root category first and all it's children. 如果您已经完成CMS中定义的所有类别,那么首先获取根类别及其所有子类别。

Category rootCategory = Category.GetRoot();
CategoryCollection childCategories = rootCategory.Categories;
foreach (Category category in childCategories)
{
    // do whatever
}

If you only want to retrieve categories selected on the current page, then iterate through the Category property on the current page. 如果您只想检索当前页面上选择的类别,则迭代当前页面上的Category属性。 It returns a CategoryList object which contains the Ids of the selected categories. 它返回一个CategoryList对象,其中包含所选类别的ID。

foreach (int catId in CurrentPage.Category)
{
    Category category = Category.Find(catId);
    // do whatever
}

Since Category.GetRoot() is marked as obsolete, this solution is more proper as per Episerver 9: 由于Category.GetRoot()被标记为过时,因此根据Episerver 9,此解决方案更合适:

var categoryRepo = ServiceLocator.Current.GetInstance<CategoryRepository>();
var rootCategory = categoryRepo.GetRoot();
CategoryCollection childCategories = rootCategory.Categories;
foreach (Category category in childCategories)
{
// do whatever
}

You use the EPiServer.DataAbstraction.Category class. 您使用EPiServer.DataAbstraction.Category类。 A good place to start would be the Category.GetRoot() method: 一个好的起点是Category.GetRoot()方法:

http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/0ace72d5-11cd-b5cc-4dbe-af38a401f528 http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/0ace72d5-11cd-b5cc-4dbe-af38a401f528

there are code examples on this page as well: 这个页面上也有代码示例:

http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/dbaa2f15-e227-1d1c-5142-2f245dd3e664 http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/dbaa2f15-e227-1d1c-5142-2f245dd3e664

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM