Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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 :-)
Thank you! you are awesome! :D
Actually I need to import 800 items, that is why i am looping through that json file.
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);
}