Working with pages in the API
The Kentico API allows you to manage pages using custom code.
Use the following classes from the CMS.DocumentEngine namespace to work with pages in the API:
- DocumentHelper - provides static methods for managing the latest edited versions of pages.
- TreeProvider - provides management functionality for latest published pages (tree nodes).
- TreeNode - represents pages. Encapsulates data from the CMS_Tree and CMS_Document tables, and the coupled data tables of individual page types.
- TreeHelper - provides static methods for loading pages (tree nodes). Allows you to work with pages without creating instances of the TreeProvider class.
This page consists of:
- Retrieving the latest edited version pages of a single Page type
- Retrieving the latest edited version of multiple or all Page types
- Retrieving the latest published version of a single Page type
- Retrieving the latest published version of multiple or all Page types
- Working with retrieved pages
For information on how you can retrieve other data from the Kentico database, see Retrieving database data using ObjectQuery API.
Retrieving the latest edited version pages of a single Page type
Use the DocumentQuery DocumentHelper.GetDocuments(string className) method to retrieve the latest edited version of a specific page type. You can also further parametrize the query.
Syntax
// Retrieve latest edited version of the 'CMS.Smartphone' pages under a specified path that have a 'Document Name' starting with 'Apple'.
var pages = DocumentHelper.GetDocuments("CMS.Smartphone")
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us")
.Published();
Query parameters
Path - You can specify the path from which you want to retrieve pages.
// Retrieve pages from the '/Services' section including the parent page. var pages = DocumentHelper.GetDocuments("CMS.MenuItem") .Path("/Services", PathTypeEnum.Section);
// Retrieve the child pages from the '/Services' section excluding the parent page. var pages = DocumentHelper.GetDocuments("CMS.MenuItem") .Path("/Services", PathTypeEnum.Children);
ExcludePath - Use to not retrieve pages under a specific path.
// Exclude the '/Products/Sale' section together with the parent page when retrieving products. var products = DocumentHelper.GetDocuments("CMS.Product") .ExcludePath("/Products/Sale", PathTypeEnum.Section);
// Exclude the child pages from the /Products/Sale section when retrieving products. var products = DocumentHelper.GetDocuments("CMS.Product") .ExcludePath("/Products/Sale", PathTypeEnum.Children);
OnSite - Specifies the site from which you want to retrieve the pages.
// Retrieve all products from the Corporate site. var products = DocumentHelper.GetDocuments("CMS.Product") .OnSite("CorporateSite");
Published - You can retrieve only pages that have been published on the live site.
// Retrieve all products published on the live site. var products = DocumentHelper.GetDocuments("CMS.Product") .OnSite("CorporateSite") .Published();
An optional false parameter in the Published method makes the query also retrieve pages that aren’t published. This includes pages that are archived or have not yet been published.
Where conditions - You can make use of different approaches to building WHERE conditions in the query.
// Retrieve smartphones that have 'Document name' starting with with 'Apple'. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Where("DocumentName", QueryOperator.Like, "Apple%");
// Retrieve smartphones that have a 'Document name' starting with with 'Apple' or 'BlackBerry'. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .Path("/Products/", PathTypeEnum.Children) .OrderBy("SmartphoneDisplaySize") .Where("DocumentName", QueryOperator.Like, "Apple%") .Or() .Where("DocumentName", QueryOperator.Like, "BlackBerry%");
// Retrieve smartphones that have 'Document name' starting with with 'Apple'. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .WhereLike("DocumentName", "Apple%");
// Retrieve smartphones that don't have 'Document name' starting with with 'Apple'. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .WhereNotStartsWith("DocumentName", "Apple ");
There are many other WHERE conditions available when building the query. Experiment to find the one that suits your needs.
Columns - Specify the columns that you want to retrieve using the Columns method.
// Retrieve smartphones based on their display size. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Columns("SmartphoneOS")
Ordering - You can order the results using some of the OrderBy methods.
// Retrieve smartphones ordered by their display size. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .OrderBy("SmartphoneDisplaySize");
// Retrieve smartphones based on their display size in a descending order. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .OrderByDescending("SmartphoneDisplaySize");
TopN - You can specify the number of records that you want to retrieve using the query.
// Retrieve the top 5 smartphone records in an ascending order based on their 'Document Name'. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .OrderByAscending("DocumentName") .TopN(5);
Paging- You can request a specific page of a certain size for a set of pages.
To make use of data paging in web parts, connect the Universal pages web part to the viewer web part.
// Retrieve the second page of size 3 of smartphone pages. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .WhereLike("DocumentName", "Apple%") .Page(1, 3);
Culture -When writing a query for a multilingual website, you will want to specify the culture of the retrieved pages. You can specify multiple cultures in a single culture method.
// Retrieve pages in two specific cultures. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("en-us", "sw-se");
CombineWithDefaultCulture - Some pages on your site may not exist in all cultures. In that case, you can make the query retrieve the default culture version instead.
// Retrieve a default culture version of a page whenever the specified culture version ('sw-se) isn't available. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("sw-se") .CombineWithDefaultCulture();
NestingLevel - You can specify the relative level that you want the query to reach within a certain section.
// Retrieve smartphones that are nested one level under the '/Products/' section. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .NestingLevel(1);
InCategory -Use the InCategory method to retrieve pages that are in specific categories.
// Retrieve smartphones that are in the 'Phablets' category. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InCategory("Phablets");
IsInRelationWith -You can retrieve pages based on their relationships with other pages.
// Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in any relationship with a specific page. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid);
// Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in a specific relationship with a specific page. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "IsRelatedTo");
// Simulate two page GUIDs. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are on the left side of a specific relationship. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "IsRelatedTo", RelationshipSideEnum.Left););
FilterDuplicates -Use this method if your site contains linked pages. This ensures that only the original pages are retrieved.
// Retrieve smartphones without duplicate (linked) pages. var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .FilterDuplicates();
CheckPermissions -You can retrieve pages based on the context of a specific user. By default, the current user’s context is used. You can apply CMSActionContext to provide a different user’s context.
// Retrieve smartphones visible to the public user. var user = UserInfoProvider.GetUserInfo("public"); using (new CMSActionContext(user)) { var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone") .Path("/Products/", PathTypeEnum.Children) .CheckPermissions(); }
Retrieving the latest edited version of multiple or all Page types
Use the MultiDocumentQuery DocumentHelper.GetDocuments() method to retrieve the latest edited version of multiple or all page types (if you don’t specify any). You can also further parametrize the query.
Syntax
// Retrieve the latest edited version of the 'CMS.Smartphone' and 'CMS.Laptop' pages under a specified path that have a 'Document Name' starting with 'Apple'.
var products = DocumentHelper.GetDocuments()
.Types("CMS.Smartphone", "CMS.Laptop")
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us")
.Published();
// Retrieve pages of all page types under a specified path that have a 'Document Name' starting with 'Apple'.
var pages = DocumentHelper.GetDocuments()
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us")
.Published();
Query parameters
Types - You can retrieve multiple page types at once.
// Retrieve pages of the 'CMS.Job' and 'CMS.Office' page type. var pages = DocumentHelper.GetDocuments() .Types("CMS.Job", "CMS.Office") .Path("/Company/", PathTypeEnum.Children);
Type - You can retrieve multiple pages types at once and specify local parameters in addition to global parameters for the whole query.
// Retrieve smartphone and laptop pages parametrized local parameters. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q.WhereLike("SmartphoneOS", "iOS 8.%")) .Type("CMS.Laptop", q => q.WhereLike("LaptopOperatingSystem", "OS X")) .Path("/Products/", PathTypeEnum.Children);
The Default method ensures that the query is parametrized only based on relevant system settings. For example, ‘Combine with default culture’.
// Retrieve smartphone and laptop pages parametrized by system settings. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q.Default()) .Type("CMS.Laptop", q => q.Default()) .Path("/Products/", PathTypeEnum.Children) .WhereLike("DocumentName", "Apple%");
Path - You can specify the path from which you want to retrieve pages.
// Retrieve pages from the /Services section including the parent page. var pages = DocumentHelper.GetDocuments() .Path("/Services/", PathTypeEnum.Section);
// Retrieve pages from the /Services section excluding the parent page. var pages = DocumentHelper.GetDocuments() .Path("/Services/", PathTypeEnum.Children);
ExcludePath - Use to not retrieve pages under an excluded path.
// Exclude the '/Products/Sale' section together with the parent page when retrieving products. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .ExcludePath("/Products/Sale", PathTypeEnum.Section);
// Exclude the child pages from the /Products/Sale section when retrieving products. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .ExcludePath("/Products/Sale", PathTypeEnum.Children);
OnSite - Specifies the site from which you want to retrieve the pages.
// Retrieve all products from the Corporate site. var products = DocumentHelper.GetDocuments() .OnSite("CorporateSite");
Published - You can retrieve only pages that have been published on the live site.
// Retrieve all pages published on the live site. var pages = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Published();
An optional false parameter in the Published method makes the query also retrieve pages that aren’t published. This includes pages that are archived or have not yet been published.
Where conditions - you can make use of different approaches to building WHERE conditions in the query.
// Retrieve all pages that have 'Document name' starting with with 'Apple'. var pages = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Where("DocumentName", QueryOperator.Like, "Apple%");
// Retrieve products that have a 'Document name' starting with with 'Apple' or 'Lenovo'. The retrieved pages are parametrized by system settings (Default method). var products = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Type("CMS.Smartphone", q => q.Default()) .Type("CMS.Laptop", q => q.Default()) .Where("DocumentName", QueryOperator.Like, "Apple%") .Or() .Where("DocumentName", QueryOperator.Like, "Lenovo%");
// Retrieve smartphones that have 'SmartphoneOS' starting with with 'iOS 8.' and laptops that have 'OS X' as 'LaptopOperatingSystem'. var products = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Type("CMS.Smartphone", q => q.WhereLike("SmartphoneOS", "iOS 8.%")) .Type("CMS.Laptop", q => q.WhereLike("LaptopOperatingSystem", "OS X"))
// Retrieve all pages on the Corporate site that don't have 'Document name' starting with with 'Apple'. var pages = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .WhereNotStartsWith("DocumentName", "Apple ");
There are many other WHERE conditions available when building the query. Experiment to find the one that suits your needs.
Note that when MultiDocumentQuery to retrieve multiple page types, you can use both local and global parameters.
Columns - You can retrieve only specific columns from each page type as well as the columns they have in common.
You can use only type-specific columns in individual Type methods.
In global parameters, you can use general document columns only.// Retrieve the 'SmartphoneOS' column from smartphones,'LaptopOperatingSystem' column from laptops and 'DocumentName' from both page types. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .Columns("SmartphoneOS") .Path("/Products/Smartphones")) .Type("CMS.Laptop", q => q .Columns("LaptopOperatingSystem") .ExcludePath("/Products/Sale")) .Columns("DocumentName");
CoupledColumns - By default, MultiDocumentQuery selects type-specific columns only if the Type query is parametrized (previous example). If you want to include coupled columns without parametrizing the Type query, use the WithCoupledColumns() method.
// Retrieve smartphones and laptops including type-specific columns. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone") .Type("CMS.Laptop") .WithCoupledColumns();
You can also include only coupled columns.
// Retrieve the 'SmartphoneOS' column from smartphones and all laptop specific columns, but no general page columns, like 'DocumentName'. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .Columns("SmartphoneOS")) .Type("CMS.Laptop") .OnlyCoupledColumns();
Ordering - You can order the results using some of the OrderBy methods.
// Retrieve smartphones and sort them based on their display size. var smartphones = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .Columns("SmartphoneDisplaySize")) .OnSite("CorporateSite") .OrderBy("SmartphoneDisplaySize");
// Retrieve smartphones and sort them by their display size in descending order. var smartphones = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .Columns("SmartphoneDisplaySize")) .OnSite("CorporateSite") .OrderByDescending("SmartphoneDisplaySize");
TopN - You can specify the number of records that you want to retrieve using the query.
// Retrieve the top 5 smartphone and top 3 laptop records in an ascending order based on their 'Document Name'. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .TopN(5)) .Type("CMS.Laptop", q => q .TopN(3)) .OrderByAscending("DocumentName");
Paging- You can request a specific page of a certain size for a set of pages.
To make use of data paging in web parts, connect the Universal pages web part to the viewer web part.
// Retrieve the first page of size 2 of smartphone and laptop pages from the '/Products/' section. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .WhereLike("DocumentName", "Apple%") .Page(0, 2);
Culture -when writing a query for a multilingual website, you will want to specify the culture of the retrieved pages. You can specify multiple cultures in a single culture method.
// Retrieve pages in two specific cultures. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("en-us", "sw-se");
CombineWithDefaultCulture -some pages on your site may not exist in all cultures. In that case, you can make the query retrieve the default culture version instead.
// Retrieve a default culture version of smartphones and laptops whenever the specified culture versions ('sw-se' or 'da-dk') aren't available. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .Culture("en-us")) .Type("CMS.Laptop", q => q .Culture("da-dk")) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .CombineWithDefaultCulture();
NestingLevel -you can specify the relative level that you want the query to reach within a certain section.
// Retrieve smartphones and laptops that are nested one level under the '/Products/' section. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .NestingLevel(1);
InCategory -Use the InCategory method to retrieve pages that are in specific categories.
// Retrieve smartphones in the 'Phablets' category and laptops in the '2in1' category. var products = DocumentHelper.GetDocuments() .Type("CMS.Smartphone", q => q .InCategory("Phablets")) .Type("CMS.Laptop", q => q .InCategory("2in1")) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children);
IsInRelationWith -You can retrieve pages based on their relationships with other pages.
// Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in any relationship with a specific page. var smartphones = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid);
// Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve products and laptops that are in the 'Complements' relationship. var complements = DocumentHelper.GetDocuments() .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "Complements");
// Simulate two page GUIDs. var nodeGuidLeft = Guid.NewGuid(); var nodeGuidRight = Guid.NewGuid(); // Retrieve products and laptops that are in the 'Complements' relationship with one another. var complements = DocumentHelper.GetDocuments() .Type("CMS.Product", q => q .InRelationWith(nodeGuidRight, "Complements", RelationshipSideEnum.Left)) .Type("CMS.Laptop", q => q .InRelationWith(nodeGuidLeft, "Complements", RelationshipSideEnum.Right)) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children);
FilterDuplicates - Use this method if your site contains linked pages. This ensures that only the original pages are retrieved.
// Retrieve smartphones and laptops without duplicate (linked) pages. var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .FilterDuplicates();
CheckPermissions - You can retrieve pages based on the context of a specific user. By default, the current user’s context is used. You can apply CMSActionContext to provide a different user’s context.
// Retrieve smartphones and laptops visible to the public user. var user = UserInfoProvider.GetUserInfo("public"); using (new CMSActionContext(user)) { var products = DocumentHelper.GetDocuments() .Types("CMS.Smartphone", "CMS.Laptop") .Path("/Products/", PathTypeEnum.Children) .CheckPermissions(); }
Retrieving the latest published version of a single Page type
Use the DocumentQuery TreeProvider.SelectNodes(string className) methodto retrieve the latest published version of a single page type. You can also further parametrize the query.
Syntax
// Create a new tree instance.
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Retrieve the latest published version of the 'CMS.Smartphone' pages under a specified path that have a 'Document Name' starting with 'Apple'.
var products = tree.SelectNodes("CMS.Smartphone")
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us");
Query parameters
Path - You can specify the path from which you want to retrieve pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages from the '/Services' section including the parent page. var pages = tree.SelectNodes("CMS.MenuItem") .Path("/Services", PathTypeEnum.Section);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the child pages from the '/Services' section excluding the parent page. var pages = tree.SelectNodes("CMS.MenuItem") .Path("/Services", PathTypeEnum.Children);
ExcludePath - Use to not retrieve pages under a specific path.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Exclude the '/Products/Sale' section together with the parent page when retrieving products. var products = tree.SelectNodes("CMS.Product") .ExcludePath("/Products/Sale", PathTypeEnum.Section);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Exclude the child pages from the /Products/Sale section when retrieving products. var products = tree.SelectNodes("CMS.Product") .ExcludePath("/Products/Sale", PathTypeEnum.Children);
OnSite - Specifies the site from which you want to retrieve the pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve all products from the Corporate site. var products = tree.SelectNodes("CMS.Product") .OnSite("CorporateSite");
Where conditions - You can make use of different approaches to building WHERE conditions in the query.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that have 'Document name' starting with with 'Apple'. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Where("DocumentName", QueryOperator.Like, "Apple%");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that have a 'Document name' starting with with 'Apple' or 'BlackBerry'. var smartphones = tree.SelectNodes("CMS.Smartphone") .Path("/Products/", PathTypeEnum.Children) .OrderBy("SmartphoneDisplaySize") .Where("DocumentName", QueryOperator.Like, "Apple%") .Or() .Where("DocumentName", QueryOperator.Like, "BlackBerry%");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that have 'Document name' starting with with 'Apple'. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .WhereLike("DocumentName", "Apple%");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that don't have 'Document name' starting with with 'Apple'. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .WhereNotStartsWith("DocumentName", "Apple ");
There are many other WHERE conditions available when building the query. Experiment to find the one that suits your needs.
Columns - Specify the columns that you want to retrieve using the Columns method.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones based on their display size. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Columns("SmartphoneOS")
Ordering - You can order the results using some of the OrderBy methods.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones ordered by their display size. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .OrderBy("SmartphoneDisplaySize");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones based on their display size in a descending order. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .OrderByDescending("SmartphoneDisplaySize");
TopN - You can specify the number of records that you want to retrieve using the query.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the top 5 smartphone records in an ascending order based on their 'Document Name'. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .OrderByAscending("DocumentName") .TopN(5);
Paging- You can request a specific page of a certain size for a set of pages.
To make use of data paging in web parts, connect the Universal pages web part to the viewer web part.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the second page of size 3 of smartphone pages. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .WhereLike("DocumentName", "Apple%") .Page(1, 3);
Culture -When writing a query for a multilingual website, you will want to specify the culture of the retrieved pages. You can specify multiple cultures in a single culture method.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages in two specific cultures. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("en-us", "sw-se");
CombineWithDefaultCulture - Some pages on your site may not exist in all cultures. In that case, you can make the query retrieve the default culture version instead.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve a default culture version of a page whenever the specified culture version ('sw-se) isn't available. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("sw-se") .CombineWithDefaultCulture();
NestingLevel - You can specify the relative level that you want the query to reach within a certain section.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that are nested one level under the '/Products/' section. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .NestingLevel(1);
InCategory -Use the InCategory method to retrieve pages that are in specific categories.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that are in the 'Phablets' category. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InCategory("Phablets");
IsInRelationWith -You can retrieve pages based on their relationships with other pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in any relationship with a specific page. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in a specific relationship with a specific page. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "IsRelatedTo");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate two page GUIDs. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are on the left side of a specific relationship. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "IsRelatedTo", RelationshipSideEnum.Left););
FilterDuplicates -Use this method if your site contains linked pages. This ensures that only the original pages are retrieved.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones without duplicate (linked) pages. var smartphones = tree.SelectNodes("CMS.Smartphone") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .FilterDuplicates();
CheckPermissions -You can retrieve pages based on the context of a specific user. By default, the current user’s context is used. To simulate a different user, create the tree instance in the context of a specific user and then parametrize the query with CheckPermissions().
// Create a new tree instance in the context of a public user. TreeProvider tree = new TreeProvider(UserInfoProvider.GetUserInfo("public"); // Retrieve smartphones visible to the public user. var smartphones = tree.SelectNodes("CMS.Smartphone") .Path("/Products/", PathTypeEnum.Children) .CheckPermissions();
Retrieving the latest published version of multiple or all Page types
Use the DocumentQuery TreeProvider.SelectNodes() methodto retrieve the latest published version of multiple pages of more than one page type. You can also further parametrize the query.
// Creates a new tree provider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Retrieves the latest published version of pages of the 'CMS.Smartphone' and 'CMS.Laptop' types a specified path that have a 'Document Name' starting with 'Apple'
var products = tree.SelectNodes()
.Types("CMS.Smartphone", "CMS.Laptop")
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us");
// Create a new tree instance.
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Retrieve the latest published version of all pages under a specified path that have a 'Document Name' starting with 'Apple'.
var products = tree.SelectNodes()
.Path("/Products/", PathTypeEnum.Children)
.WhereLike("DocumentName", "Apple%")
.ExcludePath("/Products/Sale", PathTypeEnum.Section)
.OnSite("CorporateSite")
.Culture("en-us");
Query parameters
Types - You can retrieve multiple page types at once.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages of the 'CMS.Job' and 'CMS.Office' page type. var pages = tree.SelectNodes() .Types("CMS.Job", "CMS.Office") .Path("/Company/", PathTypeEnum.Children);
Type - You can retrieve multiple pages types at once and specify local parameters in addition to global parameters for the whole query.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphone and laptop pages parametrized local parameters. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q.WhereLike("SmartphoneOS", "iOS 8.%")) .Type("CMS.Laptop", q => q.WhereLike("LaptopOperatingSystem", "OS X")) .Path("/Products/", PathTypeEnum.Children);
The Default method ensures that the query is parametrized only based on relevant system settings. For example, ‘Combine with default culture’.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphone and laptop pages parametrized by system settings. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q.Default()) .Type("CMS.Laptop", q => q.Default()) .Path("/Products/", PathTypeEnum.Children) .WhereLike("DocumentName", "Apple%");
Path - You can specify the path from which you want to retrieve pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages from the /Services section including the parent page. var pages = tree.SelectNodes() .Path("/Services/", PathTypeEnum.Section);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages from the /Services section excluding the parent page. var pages = tree.SelectNodes() .Path("/Services/", PathTypeEnum.Children);
ExcludePath - Use to not retrieve pages under an excluded path.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Exclude the '/Products/Sale' section together with the parent page when retrieving products. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .ExcludePath("/Products/Sale", PathTypeEnum.Section);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Exclude the child pages from the /Products/Sale section when retrieving products. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .ExcludePath("/Products/Sale", PathTypeEnum.Children);
OnSite - Specifies the site from which you want to retrieve the pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve all products from the Corporate site. var products = tree.SelectNodes() .OnSite("CorporateSite");
Where conditions - you can make use of different approaches to building WHERE conditions in the query.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve all pages that have 'Document name' starting with with 'Apple'. var pages = tree.SelectNodes() .OnSite("CorporateSite") .Where("DocumentName", QueryOperator.Like, "Apple%");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve products that have a 'Document name' starting with with 'Apple' or 'Lenovo'. The retrieved pages are parametrized by system settings (Default method). var products = tree.SelectNodes() .OnSite("CorporateSite") .Type("CMS.Smartphone", q => q.Default()) .Type("CMS.Laptop", q => q.Default()) .Where("DocumentName", QueryOperator.Like, "Apple%") .Or() .Where("DocumentName", QueryOperator.Like, "Lenovo%");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones that have 'SmartphoneOS' starting with with 'iOS 8.' and laptops that have 'OS X' as 'LaptopOperatingSystem'. var products = tree.SelectNodes() .OnSite("CorporateSite") .Type("CMS.Smartphone", q => q.WhereLike("SmartphoneOS", "iOS 8.%")) .Type("CMS.Laptop", q => q.WhereLike("LaptopOperatingSystem", "OS X"))
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve all pages on the Corporate site that don't have 'Document name' starting with with 'Apple'. var pages = tree.SelectNodes() .OnSite("CorporateSite") .WhereNotStartsWith("DocumentName", "Apple ");
There are many other WHERE conditions available when building the query. Experiment to find the one that suits your needs.
Note that when MultiDocumentQuery to retrieve multiple page types, you can use both local and global parameters.
Columns - You can retrieve only specific columns from each page type as well as the columns they have in common.
You can use only type-specific columns in individual Type methods.
In global parameters, you can use general document columns only.// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the 'SmartphoneOS' column from smartphones,'LaptopOperatingSystem' column from laptops and 'DocumentName' from both page types. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q .Columns("SmartphoneOS") .Path("/Products/Smartphones")) .Type("CMS.Laptop", q => q .Columns("LaptopOperatingSystem") .ExcludePath("/Products/Sale")) .Columns("DocumentName");
CoupledColumns - By default, MultiDocumentQuery selects type-specific columns only if the Type query is parametrized (previous example). If you want to include coupled columns without parametrizing the Type query, use the WithCoupledColumns() method.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones and laptops including type-specific columns. var products = tree.SelectNodes() .Type("CMS.Smartphone") .Type("CMS.Laptop") .WithCoupledColumns();
You can also include only coupled columns.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the 'SmartphoneOS' column from smartphones and all laptop specific columns, but no general page columns, like 'DocumentName'. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q .Columns("SmartphoneOS")) .Type("CMS.Laptop") .OnlyCoupledColumns();
Ordering - You can order the results using some of the OrderBy methods.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones and sort them based on their display size. var smartphones = tree.SelectNodes() .Type("CMS.Smartphone", q => q .Columns("SmartphoneDisplaySize")) .OnSite("CorporateSite") .OrderBy("SmartphoneDisplaySize");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones and sort them by their display size in descending order. var smartphones = tree.SelectNodes() .Type("CMS.Smartphone", q => q .Columns("SmartphoneDisplaySize")) .OnSite("CorporateSite") .OrderByDescending("SmartphoneDisplaySize");
TopN - You can specify the number of records that you want to retrieve using the query.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the top 5 smartphone and top 3 laptop records in an ascending order based on their 'Document Name'. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q .TopN(5)) .Type("CMS.Laptop", q => q .TopN(3)) .OrderByAscending("DocumentName");
Paging- You can request a specific page of a certain size for a set of pages.
To make use of data paging in web parts, connect the Universal pages web part to the viewer web part.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve the first page of size 2 of smartphone and laptop pages from the '/Products/' section. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .WhereLike("DocumentName", "Apple%") .Page(0, 2);
Culture -when writing a query for a multilingual website, you will want to specify the culture of the retrieved pages. You can specify multiple cultures in a single culture method.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve pages in two specific cultures. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .Culture("en-us", "sw-se");
CombineWithDefaultCulture -some pages on your site may not exist in all cultures. In that case, you can make the query retrieve the default culture version instead.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve a default culture version of smartphones and laptops whenever the specified culture versions ('sw-se' or 'da-dk') aren't available. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q .Culture("en-us")) .Type("CMS.Laptop", q => q .Culture("da-dk")) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .CombineWithDefaultCulture();
NestingLevel -you can specify the relative level that you want the query to reach within a certain section.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones and laptops that are nested one level under the '/Products/' section. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .NestingLevel(1);
InCategory -Use the InCategory method to retrieve pages that are in specific categories.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones in the 'Phablets' category and laptops in the '2in1' category. var products = tree.SelectNodes() .Type("CMS.Smartphone", q => q .InCategory("Phablets")) .Type("CMS.Laptop", q => q .InCategory("2in1")) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children);
IsInRelationWith -You can retrieve pages based on their relationships with other pages.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve smartphones that are in any relationship with a specific page. var smartphones = tree.SelectNodes() .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid);
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate a page GUID. var nodeGuid = Guid.NewGuid(); // Retrieve products and laptops that are in the 'Complements' relationship. var complements = tree.SelectNodes() .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .InRelationWith(nodeGuid, "Complements");
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Simulate two page GUIDs. var nodeGuidLeft = Guid.NewGuid(); var nodeGuidRight = Guid.NewGuid(); // Retrieve products and laptops that are in the 'Complements' relationship with one another. var complements = tree.SelectNodes() .Type("CMS.Product", q => q .InRelationWith(nodeGuidRight, "Complements", RelationshipSideEnum.Left)) .Type("CMS.Laptop", q => q .InRelationWith(nodeGuidLeft, "Complements", RelationshipSideEnum.Right)) .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children);
FilterDuplicates - Use this method if your site contains linked pages. This ensures that only the original pages are retrieved.
// Create a new tree instance. TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser); // Retrieve smartphones and laptops without duplicate (linked) pages. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .OnSite("CorporateSite") .Path("/Products/", PathTypeEnum.Children) .FilterDuplicates();
CheckPermissions - You can retrieve pages based on the context of a specific user. By default, the current user’s context is used. To simulate a different user, create the tree instance in the context of a specific user and then parametrize the query with CheckPermissions().
// Create a new tree instance in the context of a public user. TreeProvider tree = new TreeProvider(UserInfoProvider.GetUserInfo("public"); // Retrieve smartphones and laptops visible to the public user. var products = tree.SelectNodes() .Types("CMS.Smartphone", "CMS.Laptop") .Path("/Products/", PathTypeEnum.Children) .CheckPermissions();
Working with retrieved pages
You can iterate through the retrieved collection to access the properties of the individual pages. The available columns depend on how you parametrized the query when retrieving the pages.
// Retrieve smartphone pages.
var smartphones = DocumentHelper.GetDocuments("CMS.Smartphone")
.Path("/Products/", PathTypeEnum.Children)
.OnSite("CorporateSite");
// Write the 'Document Name' and 'Smartphone OS' of each of the retrieved smartphones into an HTTP output response stream.
foreach (var smartphone in smartphones)
{
var smartphoneOS = smartphone.GetValue("SmartphoneOS").ToString();
Response.Write(HTMLHelper.HTMLEncode(smartphone.DocumentName) + " - " + HTMLHelper.HTMLEncode(smartphoneOS) + "<br />");
}
Updating pages
To update a retrieved page (TreeNode object):
- Modify the page’s data:
- For general page fields, directly set the corresponding TreeNode properties.
- For the fields of specific page types, call the TreeNode.SetValue(“FieldName”, value) method.
- Call the TreeNode.Update() method.
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:
- Call the DocumentHelper.GetDocuments method with a class name parameter for a specific page type.
- Use the Types query parameter to the identify the page types and then apply the WithCoupledColumns parameter.