Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

How to set ContentPage category?

$
0
0

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<IContentRepository>();
            string json = File.ReadAllText(@"C:\my-data.json");
            PageReference parent = new PageReference(27951); 
            News allNews = JsonConvert.DeserializeObject<News>(json);
            IContentRepository contentRepository = repository;
            ContentPage newsPage = contentRepository.GetDefault<ContentPage>(parent);
            string categoryId = "";
            Category targetCategory = new Category();
            var locator = ServiceLocator.Current.GetInstance<CategoryRepository>();
            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);
            }

Viewing all articles
Browse latest Browse all 9076

Trending Articles