EPiServer 7 namespace for Locate() isn't resolving

425 views Asked by At

I'm new to EPiServer, and am attempting to retrieve child news article pages from a news list that I created. Examples that I've found online used the Locate() method, but when I attempt to apply Locate to my code, it's not being found.

This is one of the articles that I looked at. http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/8/EPiServer7-Working-with-IContentRepositoryDataFactory/

Essentially, I just need to return a list of child articles for a list of news items, so it's possible that the approach that I'm attempting is not right in the first place.

At any rate, this is my current model with the using statements.

using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.ServiceLocation;
using EPiServer.SpecializedProperties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;


namespace EPiServerExercise.Models.Pages
{
    [ContentType(DisplayName = "News List", GUID = "ac3287b1-4d78-4eb3-bad2-6b5c43530b33", Description = "")]
    public class NewsList : BasePage
    {

        private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
        {


            //var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>


            //IEnumerable<NewsArticle> newsArticles = new List<NewsArticle>();
            //PageReference pageLink = currentPage.ParentLink;
            //IEnumerable<NewsArticle> newsArticles = Locate.ContentRepository().GetChildren<IContent>(pageLink);
            //IEnumerable<NewsArticle> newsArticles = ServiceLocationHelperExtensions.
            //var serviceLocationHelper = ServiceLocator.Current.GetInstance();
            //serviceLocationHelper.ContentLoader

        }

    }
}

What reference am I missing to get the Locate() method to resolve? We are using EPiServer 7 and MVC. Thanks for your help.

Update on 11/18/2014

This is the eventual solution that I put into the model. It's almost identical to what Vsevolod Goloviznin suggested. Thanks.

public string showNewsArticles()
{
    IEnumerable<NewsArticle> newsArticles = getNewsArticles(this);
    // Code to loop through the articles
}

private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
{
    var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
    IEnumerable<NewsArticle> newsArticles = repository.GetChildren<NewsArticle>(currentPage.ContentLink);
    return newsArticles;
}
1

There are 1 answers

1
Vsevolod Goloviznin On BEST ANSWER

Looks like he just uses a factory to get IContentRepository and forgot to mention to. So to just get the same functionality you can use the ServiceLocator to get the IContentRepository and then get all children for your page:

var service = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
var pages = service.GetChildren<NewsArticle>(currentPage.ParentLink).ToList();