The Xperience API allows you to retrieve and manage page content using custom code.
Use the following API to work with pages:
Page query parametrization methods
For detailed information about the options that allow you to parameterize page queries, see the dedicated reference: Reference - DocumentQuery methods
Loading other objects
To learn how to retrieve other types of non-page data from the Xperience database, see Retrieving database data using ObjectQuery API.
Tip: Optimize the performance of your website by caching the retrieved page data.
For basic retrieval of pages in the code of your live site, use the IPageRetriever service. The service is available in the Kentico.Content.Web.Mvc namespace, which is provided by the NuGet packages installed in your live site application.
The IPageRetriever methods return a collection of TreeNode objects representing the given pages (or a specific page type class inheriting from TreeNode).
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));
}IPageRetriever automatically performs some parameterization of the query to simplify the code for the most common scenarios and requires live site context to function. However, if writing outside of your web application or if you require full control over the query, you can directly use the DocumentQuery or MultiDocumentQuery classes to ensure that there is not any unexpected parametrization done in the background.
// Retrieves all pages that match the query
var pages = new MultiDocumentQuery()
.OnCurrentSite()
.Path("/Articles/Reviews")
.Culture("en-us")
.CombineWithDefaultCulture()
.PublishedVersion()
.WithCoupledColumns();// Retrieves pages of the specified page type that match the query
var pages = new DocumentQuery("Custom.Smartphone")
.OnCurrentSite()
.Path("/Products")
.Culture("en-us")
.CombineWithDefaultCulture()
.PublishedVersion();When retrieving pages under workflow, you may want to retrieve the latest edited version in some cases and the published version in others.
Note: The Published parametrization method does not specify the version of the pages you retrieve, instead it limits the retrieved pages to only those that are currently published according to the value of their Published from / Published to settings and have a published version.
When retrieving page content using DocumentQuery or MultiDocumentQuery in the code of your website application for the preview mode or the page builder interface, you want to get the latest version of the content. However, you still need the published version to be displayed on the live site. To achieve this, you can parametrize queries to conditionally serve a different version of content based on where the content is displayed.
Tip: If you do not specifically require DocumentQuery or MultiDocumentQuery for a custom scenario, it is recommended to use the IPageRetriever service, which is preconfigured to provide the correct version of content.
using CMS.DocumentEngine;
using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;
// Checks whether the current page is displayed in preview mode or the edit mode of the page builder
bool showLatest = HttpContext.Kentico().PageBuilder().EditMode || HttpContext.Kentico().Preview().Enabled;
// Retrieves the latest edited version of pages in preview mode or the page builder, but the published version in other cases
var pages = new DocumentQuery("Custom.Smartphone")
.OnCurrentSite()
.Culture("en-us")
.LatestVersion(showLatest);You can iterate through the retrieved collection to access the properties of individual pages. The available columns depend on how you parametrized the query when retrieving the pages.
// Retrieves smartphone pages
var smartphones = new DocumentQuery("Custom.Smartphone")
.OnCurrentSite()
.Culture("en-us")
.Path("/Products/", PathTypeEnum.Children)
.LatestVersion();
// Writes the 'DocumentName' and 'SmartphoneOS' values of each of the retrieved smartphones into an HTTP output response stream
foreach (TreeNode smartphone in smartphones)
{
string smartphoneOS = smartphone.GetValue<string>("SmartphoneOS", "Default OS");
Response.Write(HTMLHelper.HTMLEncode(smartphone.DocumentName) + " - " + HTMLHelper.HTMLEncode(smartphoneOS) + "<br />");
}Tip: To see more examples of how to work with pages using the Xperience API, see the API Examples documentation.
To update a page (TreeNode object):
For examples and additional scenarios, see the API Examples documentation.
Updating pages under workflow
When using the API to update pages under workflow or versioning, always retrieve the page objects with all fields. Otherwise, the update may cause data loss. Use one of the following approaches:
To create a page:
For examples and additional scenarios, see the API Examples documentation.