You should be able to just do:
var myCategory = new Category(topCategory, catname);
Hi Johan,
THat is where I took the code from - use CreateWritableClone then Save.
However this doesn't seem to update the ID - so all you actually do is to amend the category you accessed, it doesn't create a new instance.
So this code:
var topCategory = categoryRepository.Get("TopLevel"); // Returns a read-only instance
var myCategory = topCategory.CreateWritableClone();
Guid newGuid = Guid.NewGuid();
myCategory.GUID = newGuid;
myCategory.Parent = topCategory;
myCategory.Name = catname;
myCategory.Description = catname + " Created by data takeon";
myCategory.Available = true;
myCategory.Selectable = true;
categoryRepository.Save(myCategory);
return myCategory;
Should create a new category under the root category TopLevel - but instead renames the TopLevel category to a new name - which means the next time this runs it fails as TopLevel is no longer there.
In fact the example in the link has this slight difference
var topCategory = categoryRepository.Get("TopLevel"); // Returns a read-only instance
var myCategory = topCategory.CreateWritableClone();
Guid newGuid = Guid.NewGuid();
myCategory.GUID = newGuid;
myCategory.Parent = topCategory;
myCategory.Name = catname;
myCategory.Description = catname + " Created by data takeon";
myCategory.Available = true;
myCategory.Selectable = true;
topCategory.Save(myCategory);
return myCategory;
where the save is against the Repository - but that then throws a compilation error - "No overload for method Save takes 1 arguments"
Creating a writable clone is only needed when updating an existing item.
A new item only needs the constructor I exemplified.
Since a category must have a parent you need to look one up and set as parent. You shouldn't do anything else with "topCategory".
The problem is that the constructor you specified doesn't work under EpiServer 8.
var myCategory = new Category(topCategory, catname);
This throws an exception stating CreateWritableClone must be used.
Trung at EPiServer has now come up with a workable solution:
var categoryRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<EPiServer.DataAbstraction.CategoryRepository>();
var myCategory = categoryRepository.Get("Meet"); // Returns a read-only instance
myCategory = myCategory.CreateWritableClone();
myCategory.Description = "My category is really nice";
var childCategory = new Category(myCategory, "Child");
categoryRepository.Save(myCategory);
categoryRepository.Save(childCategory);
It seems as if both the parent and child need to be saved (in that order).
I have tested this and all seems well.
Thanks to all.
David
Hi - I am trying to create new categories in EpiServer 8 using C#. The API has changed so that I now have thiis code:
var topCategory = categoryRepository.Get("TopLevel"); // Returns a read-only instance
var myCategory = topCategory.CreateWritableClone();
Guid newGuid = Guid.NewGuid();
myCategory.GUID = newGuid;
myCategory.Parent = topCategory;
myCategory.Name = catname;
myCategory.Description = catname + " Created by data takeon";
myCategory.Available = true;
myCategory.Selectable = true;
categoryRepository.Save(myCategory);
return myCategory;
This is because I have to use CreateWritableClone. However this leaves the ID unchanged, so all that happens is that I return the category called TopLevel (ID = 17), but then all it does is update its properties and saves as ID = 17. I want to be able to create a new category as a child of TopLevel, but can't see how to do this.
Anyone done this in Episerver 8?
Regards
David