Is this add category programmatically function a bug?

Vote:
 

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();
newsPage.Category.Add(2);
newsPage.Category.Add(3);
newsPage.Category.Add(4);

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

#149284
May 27, 2016 15:39
Vote:
 

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";
categoryRepository.Save(myCategory);
#149286
May 27, 2016 16:03
Vote:
 

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

#149287
May 27, 2016 16:06
Vote:
 

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");
            clone.Category.Add(alloyTrackCategory.ID);
            contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
#149289
May 27, 2016 16:31
Vote:
 

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

#149296
May 27, 2016 17:27
Vote:
 

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

#149300
Edited, May 27, 2016 17:49
Vote:
 

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: http://world.episerver.com/Forum/Developer-forum/-EPiServer-75-CMS/Thread-Container/2014/9/Add-Category-to-a-page/

#149307
May 29, 2016 14:09
Vote:
 

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().

#149324
May 30, 2016 7:46
Vote:
 

If you check .Add it does this in the background

 public void Add(int category)
    {
      this.ThrowIfReadOnly();
      if (this.Contains(category))
        return;
      this._categories.Add(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?

#150215
Edited, Jun 14, 2016 9:09
Vote:
 

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.

#150216
Jun 14, 2016 9:47
Vote:
 

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

Sweet!

#150217
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.