November Happy Hour will be moved to Thursday December 5th.

How to set ContentPage category?

Vote:
 

Hi,

I was wondering how can I set a category for a ContentPage. I have the following code but when I try to assign a value to category it gives me the following error:

Cannot implicitly convert type 'EPiServer.DataAbstraction.Category' to 'EPiServer.Core.CategoryList' Pure.Core 

And here is my code:
(in this line I have the issue: newsPage.Category = targetCategory;)

var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
            string json = File.ReadAllText(@"C:\my-data.json");

            PageReference parent = new PageReference(27951); 
            News allNews = JsonConvert.DeserializeObject(json);
            IContentRepository contentRepository = repository;
            ContentPage newsPage = contentRepository.GetDefault(parent);

            string categoryId = "";
            Category targetCategory = new Category();

            var locator = ServiceLocator.Current.GetInstance();
            var rootCategory = locator.GetRoot();
            CategoryCollection childCategories = rootCategory.Categories;
            foreach (Category category in childCategories)
            {
                CategoryCollection childCategoriesInside = category.Categories;
                foreach (Category childCategory in childCategoriesInside)
                {
                    if (childCategory.Name == "Sample name")
                    {
                        categoryId = childCategory.ID.ToString();
                        targetCategory = childCategory;
                    }
                }
            }

            foreach (NewsItem item in allNews.data)
            {
                newsPage.Name = item.title;
                newsPage.Published = Convert.ToDateTime(item.date);
                newsPage.Changed = Convert.ToDateTime(item.date);
                newsPage.MainContent = new XhtmlString(item.body);
                newsPage.Category = targetCategory;

                contentRepository.Save(newsPage, SaveAction.Publish, AccessLevel.Read);
            }
#178856
May 24, 2017 12:55
Vote:
 

You don't need to loop through all the categories, you could just do a Get():

           var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
            var targetCategory = categoryRepository.Get("Sample name");

Assign the values like this:

                newsPage.Category = new CategoryList();
                newsPage.Add(targetCategory);

By the way, it looks like you are creating a single page, and then assigning the values over and over to it. You'll probably have other issues with your code, but I'm sure you'll figure it out :-)

#178858
May 24, 2017 14:04
Vote:
 

Thank you! you are awesome! :D

Actually I need to import 800 items, that is why i am looping through that json file.

#178859
May 24, 2017 14:20
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.