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);
Don't think I follow, I'm not trying to create categoies, I'm assigning them to a page...
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);
i'm sorry bit I think that you have missunderstood the question. Please read it again :)
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
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/
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().
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?
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.
I've noticed that when adding categories to a page programmatically like this, Episerver doesn't save any categories.
But if I save categories like this Episerver succeeds in saving the categories
Would be great if someone could verify or not verify that it actually is like this.