Is it possible to assign the CategoryID value to CategoryInfo object?

Francisco Caicedo asked on January 25, 2016 12:15

I've been trying to add a new category by using the following block of code:

CategoryInfo category = new CategoryInfo()
{
    CategoryID = 999, // manually set
    CategoryName = "TestCategory",
    CategoryDisplayName = "Test Category",
    CategoryEnabled = true,
    CategorySiteID = 1
};

CategoryInfoProvider.SetCategoryInfo(category);

This doesn't throw any errors but it doesn't add the new category to CMS_Category table.

However, if I removed this line: CategoryID = 999,, the category gets saved into the system and the CategoryID is automatically assigned.

I would like to set this field manually. Any help is appreciated. (I am trying to avoid creating additional fields to handle this)

Recent Answers


Martin Hejtmanek answered on January 25, 2016 15:49

Hi, you cannot specify the primary key manually. When an object has the primary key set, the operation is recognized as update. As category with ID 999 does not exist, the update doesn't update anything.

What is the reason why you want to specify the primary key manually?

0 votesVote for this answer Mark as a Correct answer

Francisco Caicedo answered on January 26, 2016 10:27

Hello, I want to be able to control CategoryID values because I need to manually match categories and subcategories (CategoryParentID) from an external source. At the moment, if I cannot set up the value for each category. I won't be able to identify the dependency with the subcategories. Is there a way to achieve this manual set up? I also noticed I cannot create custom fields in CMS_Category. Thank you.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.