Different between IPageRetriever and IPageDataContextRetriever

Stephen Herz asked on July 15, 2021 23:12

I'm taking the Kentico Xperience 13 for Developers training course which uses the MedioClinic site. I'm struggling to understand what are the difference between the IPageRetriever and IPageDataContextRetriever services. Can someone help explain when to use each and the differences between them?

Correct Answer

Liam Goldfinch answered on July 16, 2021 01:47

In previous versions of Kentico (and still in KX13 if you want to use it) you'd use DocumentQuery to build up queries to retrieve pages.

IPageRetriever is essentially a wrapper around DocumentQuery - it lets you retrieve pages but makes it easier by automatically ensuring you're retrieving the right version with the current site and current culture. Example taken from here.

private readonly IPageRetriever pageRetriever;

// Gets an instance of the IPageRetriever service using dependency injection
public ExampleController(IPageRetriever pageRetriever)
{
    this.pageRetriever = pageRetriever;
}

public ActionResult Index()
{
    // Retrieves pages of the 'Article' page type that are in the '/Articles/May' section of the content tree
    var articles = pageRetriever.Retrieve<Article>( documentQuery => documentQuery
                    .Path("/Articles/May", PathTypeEnum.Children));

    // Retrieves pages of a custom 'Landing page' page type from the '/Landing-pages/Products' section of the content tree
    var landingPages = pageRetriever.Retrieve("Custom.LandingPage"( documentQuery => documentQuery
                    .Path"/Landing-pages/Products", PathTypeEnum.Children));

    // Retrieves pages of multiple page types from the '/Archive' section of the content tree
    var pages = pageRetriever.RetrieveMultiple( multiDocumentQuery => multiDocumentQuery
                    .Path("/Archive", PathTypeEnum.Children));
}

IPageDataContextRetriever is used within Controllers/ViewComponents to access the currently requested page. Example taken from here.

private readonly IPageDataContextRetriever dataRetriever;

public ArticlesController(IPageDataContextRetriever dataRetriever)
{
    // Initializes an instance of a service that provides data context of pages matching the requested URL
    this.dataRetriever = dataRetriever;
}

public ActionResult Show()
{
...
    // Gets the page of the Article page type matching the currently requested URL
    var article = dataRetriever.Retrieve<Article>().Page;
...
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Sean Wright answered on July 19, 2021 17:36

Like Liam mentioned, one is for retrieving Pages (IPageRetriever) and one is for retrieving the information about the Page associated with the current request (IPageDataContextRetriever).

Any service/type that has context in the name is going to refer to information specific to the current request (like IHttpContextAccessor).

If you are using Content Tree based routing then Xperience will populated the value returned by the IPageDataContextRetriever because it understands the relationship between the request URL and the Content Tree.

If you are using custom routing then you will need to populate the 'context' yourself programmatically using the IPageDataContextInitializer.

0 votesVote for this answer Mark as a Correct answer

Stephen Herz answered on July 19, 2021 23:40

Got it now. Thank you both for clarifying that.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.