The category code name MUST be unique, but the category display name can be anything.
Category code Name in terms of SQL is CategoryName and it is clustered index. Where in the tree you create a category is not important, but CategoryName must be unique.
So CheckUniqueCodeName runs SQL query and returns true or false, if have 2 records in the DB like above I try to create a new category under TEST:
// Gets the parent category
CategoryInfo parentCategory = CategoryInfoProvider.GetCategoryInfo("TEST", SiteContext.CurrentSiteName);
if (parentCategory != null)
{
// Creates a new category object
CategoryInfo subcategory = new CategoryInfo();
// Sets basic properties
subcategory.CategoryDisplayName = "SOMENEWCATEGORY";
subcategory.CategoryName = "TEST";
subcategory.CategorySiteID = SiteContext.CurrentSiteID;
subcategory.CategoryEnabled = true;
// Assigns to the parent category
subcategory.CategoryParentID = parentCategory.CategoryID;
Response.Write("is Unique: " + subcategory.CheckUniqueCodeName());
subcategory.CategoryName = "TEST123123";
Response.Write("is Unique: " + subcategory.CheckUniqueCodeName());
}
I will get
is Unique: False
is Unique: True
I think you a have a logical problem here: you probably create categories in a loop or something and you need to move out your Random random = new Random();
out of your loop. I think that your random returns the same value.