Is this really the version you are looking for? Kentico Xperience 12 Service Pack Documentation is no longer updated. Use the switcher in the header to choose your product version.
The Kentico API allows you to retrieve and manage page content using custom code.
Use the following classes from the CMS.DocumentEngine namespace to work with pages in the API:
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 Kentico database, see Retrieving database data using ObjectQuery API.
For basic retrieval of pages in your code, use the DocumentHelper.GetDocuments method and parameterize it using parametrization methods to only include the pages you need. If you have generated page type providers available, you can use them with the same parametrization methods, and then work with strongly typed page objects.
// Retrieves the published English version of articles that are in the '/Articles/May' section of the 'MySite' site's content tree
// Only loads pages that are currently published on the live site according to the value of their Published from/to setting
var pages = DocumentHelper.GetDocuments("MySite.Articles")
.OnSite("MySite")
.Culture("en-us")
.Path("/Articles/May", PathTypeEnum.Children)
.PublishedVersion()
.Published();When working with 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 in the code of your MVC application for the preview mode or the page builder interface, you want to get the latest version of the content, but you want 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.
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
DocumentQuery pages = DocumentHelper.GetDocuments("Custom.Smartphone")
.LatestVersion(showLatest);DocumentHelper automatically performs some parameterization of the query to simplify the code for the most common scenarios. However, 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")
.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")
.PublishedVersion();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
DocumentQuery smartphones = DocumentHelper.GetDocuments("CMS.Smartphone")
.Path("/Products/", PathTypeEnum.Children)
.OnSite("CorporateSite");
// 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 />");
}To update a page (TreeNode object):
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: