Create a new category issue

Vote:
 

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

#142707
Dec 18, 2015 17:54
Vote:
 

You should be able to just do:

var myCategory = new Category(topCategory, catname);

http://world.episerver.com/documentation/Items/Upgrading/EPiServer-CMS/8/Breaking-changes/changes-to-the-category-api/

#142708
Dec 18, 2015 22:29
Vote:
 

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.

#142839
Jan 04, 2016 10:34
Vote:
 

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"

#142840
Jan 04, 2016 10:40
Vote:
 

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". 

#142855
Jan 04, 2016 12:59
Vote:
 

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.

#142856
Jan 04, 2016 13:02
Vote:
 

Can you post the code where you aren't using CreateWritableClone? 

#142862
Jan 04, 2016 13:08
Vote:
 

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

#142931
Jan 06, 2016 12:51
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.