November Happy Hour will be moved to Thursday December 5th.

Importing category data to Episerver CMS

Vote:
 

We have a taxonomy managed by our organisation, and the taxonomy is stored in a file. The taxonomy is a tree consisting of categories (for example School category has subcategories Primary School and Secondary School, and Primary School has subcategories State Primary School and Private Primary School and so on). We would like Episerver to import the taxonomy, and use the taxonomy to categorise pages in Episerver for search etc. If the taxonomy changes in future, Episerver can reimport the taxonomy. Any suggestion is welcome. Thanks!

#251310
Mar 24, 2021 14:54
Vote:
 

Hi,

You can create a scheduled job that will read your file and based file data it will create the categories in the Episerver CMS

Example for creating a scheduled job-

https://blog.tech-fellow.net/2020/12/07/episerver-scheduled-jobs-under-the-hood/

Sample code to create categories-

private void CreateIfNotExists(string name, string description, int order)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException("description");
            }

            var categories = CategoryList.LoadCategories();
            if (categories.Contains(name))
            {
                return;
            }

            var category = new Category(name, description)
                           {
                                   Available = true,
                                   Parent = Category.GetRoot(),
                                   SortOrder = order
                           };

            category.Save();
        }

FYI - If you have a multilingual site then you can use Geta Categories

https://github.com/Geta/EpiCategories

#251339
Edited, Mar 25, 2021 6:48
Jianhan Zhu - Mar 25, 2021 10:11
Thanks for your helpful suggestions. Where to put the code to create categories, in the Initialization section, or as another scheduled job?
Ravindra S. Rathore - Mar 25, 2021 10:12
In the Scheduled Job
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.