For developers: Query pages
This topic explains how to query instances of page nodes. The examples below show you how to query the following:
To find a specific page you can use the Native API or the Fluent API.
To find a specific page using the Native API, you use the PageManager class and LINQ queries. For more information, see Find pages using Native API.
To find a specific page using the Fluent API, you use the page facade. For more information, see Find pages using Fluent API.
Find pages using Native API
Find page node by ID
To find a specific page node by ID, you use the GetPageNodes method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetPageNode_NativeAPI |
|
{ |
|
public PageNode FindPageNodeByIdNativeAPI(Guid pageNodeId) |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
PageNode node = pageManager.GetPageNodes().Where(n => n.Id == pageNodeId).FirstOrDefault(); |
|
return node; |
|
} |
|
|
|
} |
|
} |
To find the page node, you use the GetPageNodes method and filter based on the Id property. If the page node does not exist, the method returns null.
To find the page node, you can also use the GetPageNode method: PageNode node = pageManager.GetPageNode(pageNodeId);
The GetPageNode method throws an exception of type ItemNotFoundException, if there is no page node with the specified Id.
Find page data by ID
To find a specific page data by ID, you use the GetPageDataList method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPageDataById_NativeAPI |
|
{ |
|
public PageData FindPageDataByIdNativeAPI(Guid pageDataId) |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
PageData page = pageManager.GetPageDataList().Where(p => p.Id == pageDataId).FirstOrDefault(); |
|
return page; |
|
} |
|
} |
|
} |
To find the page data, you use the GetPageDataList method and filter based on the Id property. If the page data does not exist, the method returns null.
To find the page data, you can also use the GetPageData method:
PageData page = pageManager.GetPageData(pageDataId);
The GetPageData method throws an exception of type ItemNotFoundException, if there is no page data with the specified Id.
PageData now has a NavigationNodeproperty that holds the reference to the page metadata (located in the PageNode). Previously, the PageNode had a Page property (of typePageData that is now obsolete). The main impact of this change is on Multilingual Split pages. You now get the PagaData of the PageNode in a different way:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_PageData_NativeAPI |
|
{ |
|
public static PageData FindPageByTitleNativeAPI(string title) |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
PageData page = pageManager.GetPageDataList().Where(pData => pData.NavigationNode.Title == title).FirstOrDefault(); |
|
return page; |
|
} |
|
} |
|
} |
Find page data by URL of node
To find a specific page data by the URL of the node, first, you use the GetPageNodes method. Then you filter based on the UrlName property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPagebyUrl_NativeAPI |
|
{ |
|
public PageData FindPagebyUrlNativeAPI(string urlName) |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
PageNode pageNode = pageManager.GetPageNodes().Where(pN => pN.UrlName == urlName).FirstOrDefault(); |
|
|
|
if (pageNode == null) |
|
{ |
|
return null; |
|
} |
|
|
|
return pageNode.GetPageData(); |
|
} |
|
} |
|
} |
Get pages by template
To get pages by a specified template you must first get an instance of the LibrariesManager object. Then, you must get an instance of the specified template. To get the pages using this template, call the Pages extension method of the PageTemplate instance. This extension method is located in the Telerik.Sitefinity.Modules.Pages namespace. Here is a code example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetPagesByTemplate_NativeAPI |
|
{ |
|
public static IQueryable<PageData> GetPagesByTemplateNativeAPI(Guid templateId) |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
|
|
PageTemplate template = pageManager.GetTemplate(templateId); |
|
|
|
return template.Pages().Where(p => p.Status == ContentLifecycleStatus.Live); |
|
} |
|
} |
|
} |
Get all live pages
To get all live pages, first, you use the GetPageDataList method. Then, you filter based on the Status property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetLivePages_NativeAPI |
|
{ |
|
public IQueryable<PageData> GetLivePagesNativeAPI() |
|
{ |
|
PageManager pageManager = PageManager.GetManager(); |
|
IQueryable<PageData> pages = pageManager.GetPageDataList().Where(pData => pData.Status == ContentLifecycleStatus.Live); |
|
return pages; |
|
} |
|
} |
|
} |
Find pages using Fluent API
To find a page using the Fluent API, you must use the page facade.
To initialize the page facade, you use App.WorkWith().Page() for the singular page facade or App.WorkWith().Pages() for the plural page facade.
Find page node by ID
To find a specific page node by ID, first, you use the plural page facade to assure that the page node exists. Then, you use the Get method of the singular facade to get the page node:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPageNodeById_FluentAPI |
|
{ |
|
public PageNode FindPageNodeByIdFluentAPI(Guid pageNodeId) |
|
{ |
|
PageNode node = null; |
|
|
|
var count = 0; |
|
|
|
App.WorkWith().Pages().Where(pN => pN.Id == pageNodeId).Count(out count); |
|
|
|
if (count != 0) |
|
{ |
|
node = App.WorkWith().Page(pageNodeId).Get(); |
|
} |
|
|
|
return node; |
|
} |
|
} |
|
} |
If you do not assure that the page node exists, the
Get method throws an exception of type
ItemNotFoundException, if there is no page node with the specified
Id.
Find page data by node ID
To find a specific page data by its node ID, first, you use the plural page facade to assure that the page node exists. Then, you use the Get method of the singular facade to get the page node. To get the PageData, you use the Page property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPageDataByNode_FluentAPI |
|
{ |
|
public PageData FindPageDataByNodeFluentAPI(Guid pageNodeId) |
|
{ |
|
PageData pageData = null; |
|
|
|
var count = 0; |
|
|
|
App.WorkWith().Pages().Where(pN => pN.Id == pageNodeId).Count(out count); |
|
|
|
if (count != 0) |
|
{ |
|
pageData = App.WorkWith().Page(pageNodeId).Get().GetPageData(); |
|
} |
|
|
|
return pageData; |
|
} |
|
} |
|
} |
If you do not assure that the page data exists, the Get method throws an exception of type ItemNotFoundException, if there is no page data with the specified Id.
Find page data by title
To find a specific page data by its title, first, you use the plural page facade to assure that the page node with the specified title of the PageData exists. Then, you use the Get method of the plural page facade to get the page node. To get the PageData, you use the Page property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPageByTitle_FluentAPI |
|
{ |
|
public PageData FindPageByTitleFluentAPI(string pageTitle) |
|
{ |
|
PageData pageData = null; |
|
|
|
var count = 0; |
|
App.WorkWith().Pages().Where(pN => (pN.Page != null && pN.Page.Title == pageTitle)).Count(out count); |
|
|
|
if (count != 0) |
|
{ |
|
pageData = App.WorkWith().Pages().Where(pN => (pN.Page != null && pN.Page.Title == pageTitle)).Get().First().Page; |
|
} |
|
|
|
return pageData; |
|
} |
|
} |
|
} |
Find page data by URL of the node
To find a specific page data by the URL of the node, first, you use the plural page facade to assure that the page node with the specified UrlName exists. Then, you use the Get method of the plural page facade to get the page node. To get the PageData, you use the Page property:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_FindPagebyUrl_FluentAPI |
|
{ |
|
public PageData FindPagebyUrlFluentAPI(string urlName) |
|
{ |
|
PageData pageData = null; |
|
|
|
var count = 0; |
|
App.WorkWith().Pages().Where(pN => pN.UrlName == urlName).Count(out count); |
|
|
|
if (count != 0) |
|
{ |
|
pageData = App.WorkWith().Pages().Where(pN => pN.UrlName == urlName).Get().First().Page; |
|
} |
|
|
|
return pageData; |
|
} |
|
} |
|
} |
Get pages by template
To get the pages that use a specified template you must first get the plural facade for the pages. Then filter the pages by the ID of the specified template and their status. Call the Get method of the facade to get the query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QuesryPages_GetPagesByTemplate_FluentAPI |
|
{ |
|
public static IQueryable<PageData> GetPagesByTemplateFluentAPI(Guid templateId) |
|
{ |
|
return App.WorkWith().Pages().Where(p => p.GetPageData() != null && p.GetPageData().Template.Id == templateId && p.GetPageData().Status == ContentLifecycleStatus.Live).Get().Select(p => p.GetPageData(null)); |
|
} |
|
} |
|
} |
Get all live pages
To get all live pages, first, you use the plural facade to get all page nodes. Then, you filter based on the Status property of the PageData object:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Pages.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetLivePages_FluentAPI |
|
{ |
|
public IQueryable<PageNode> GetLivePagesFluentAPI() |
|
{ |
|
IQueryable<PageNode> pageNodes = App.WorkWith().Pages().Where(pN => (pN.GetPageData() != null && pN.GetPageData().Status == ContentLifecycleStatus.Live)).Get(); |
|
return pageNodes; |
|
} |
|
} |
|
} |
Get all translations of a page
Native API
To get all translations of a page, you use the
GetPageDataList method. Then, you filter based on the
Culture property, the
LocalizationStrategy,
NodeType and
RootNodeId. As a result, you get all pages for a specific language:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using System.Threading; |
|
using Telerik.Sitefinity.Modules.Pages; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetAllTranslationsOfPage_NativeAPI |
|
{ |
|
public void GetAllTranslationsOfPage() |
|
{ |
|
var cultureInfo = new System.Globalization.CultureInfo("cz"); |
|
var currentCulture = Thread.CurrentThread.CurrentUICulture; |
|
var ci = System.Globalization.CultureInfo.GetCultureInfo("cz"); |
|
var pageManager = PageManager.GetManager(); |
|
var pages = pageManager.GetPageDataList().Where(pageData => |
|
(pageData.Culture == "cz" || |
|
pageData.NavigationNode.LocalizationStrategy != Telerik.Sitefinity.Localization.LocalizationStrategy.Split) && |
|
pageData.NavigationNode.NodeType == Telerik.Sitefinity.Pages.Model.NodeType.Standard && |
|
pageData.NavigationNode.RootNodeId == Telerik.Sitefinity.Abstractions.SiteInitializer.CurrentFrontendRootNodeId) |
|
.Select(x => x.NavigationNode); |
|
var filteredTranslations = pages.ToList(); |
|
} |
|
|
|
} |
|
} |
Fluent API
To get all live pages, first, you use the plural facade to get all page nodes. Then, you filter based on the
NodeType property and the
PageData object:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Globalization; |
|
using Telerik.Sitefinity; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class QueryPages_GetAllTranslationOfPage_FluentAPI |
|
{ |
|
public void QueryPages_GetAllTranslationsOfPage_FluentAPI(CultureInfo ci) |
|
{ |
|
var childNodes = App.WorkWith() |
|
.Pages() |
|
.LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend) |
|
.Where(p => p.GetPageData() != null && p.NodeType == Telerik.Sitefinity.Pages.Model.NodeType.Standard) |
|
.ThatArePublished() |
|
.ThatAreForLanguage(ci) |
|
.Get(); |
|
} |
|
} |
|
} |