Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Is this add category programmatically function a bug?


I've noticed that when adding categories to a page programmatically like this, Episerver doesn't save any categories.

newsPage.Property.Set("PageCategory", new PropertyCategory());
newsPage.Category = new CategoryList(new int[] { 2, 3, 4 });

But if I save categories like this Episerver succeeds in saving the categories

newsPage.Property.Set("PageCategory", new PropertyCategory());
newsPage.Category = new CategoryList();

Would be great if someone could verify or not verify that it actually is like this.

May 27, 2016 15:39

Categories there are read only to save memory and performance. Use 

var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
var myCategory = categoryRepository.Get("MyCategory"); // Returns a read-only instance
myCategory = myCategory.CreateWritableClone();
myCategory.Description = "My category is really nice";
May 27, 2016 16:03

Don't think I follow, I'm not trying to create categoies, I'm assigning them to a page...

May 27, 2016 16:06

Ah! If you just want to add categories it's similar. PageData is read only to save memory and performance so first need to make it a writable clone like this:

            var clone = currentPage.CreateWritableClone();
            var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var alloyTrackCategory = categoryRepository.Get("Track");
            contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
May 27, 2016 16:31

i'm sorry bit I think that you have missunderstood the question. Please read it again :)

May 27, 2016 17:27

Hehe sorry...was coding another project with the read part of the brain obviously :)

Yet another example of why you shouldn't multi-task...

I'll check on it

Edited, May 27, 2016 17:49

The line newsPage.Property.Set("PageCategory", new PropertyCategory()); makes no sense in any of the examples.

I usually have a CategoryList instatiated before and do add on it based on some checkbox list etc and then set it to page.Category. Similiar to the example in this thread:

May 29, 2016 14:09

Hi Johan,

I don't know how to be more clear. The question is not about the .Add() functionality, it works. The question is whether or not page.Category = new CategoryList(intArray) should work the same was as page.Add(). Cause right now if I do new CategoryList() episerver does not persist the categories, it only persists the values when doing .Add().

May 30, 2016 7:46

If you check .Add it does this in the background

 public void Add(int category)
      if (this.Contains(category))
      this._isSorted = false;
      this._isModified = true;

Note that it sets the_isModified flag...
If we check the constructor it does this...

public CategoryList(int[] categories)
: this(0)
this._categories.AddRange((IEnumerable<int>) categories);
this._isSorted = false;

Note that it doesn't set _isModified flag. The AddRange call won't either btw. This flag comes from the interface IModifiedTrackable which controls whether Episerver should save it when persisting the IContent. 

So you are correct.
Bug. You want to report it?

Edited, Jun 14, 2016 9:09

Thanks for the confirmation :) It sure looks like a bug (or a very unclear documentation). Since you did the most work you are more then welcome to report it, otherway I'll do it.

Jun 14, 2016 9:47

I'll report it so they can fix it. It's a one liner for them so...


Jun 14, 2016 9:50
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.