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 Finding a page using Native API.
To find a specific page using the Fluent API, you use the page facade. For more information, see Finding a page using Fluent API.
Finding a page using Native API
Finding a page node by ID
To find a specific page node by ID, you use the GetPageNodes method:
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.
Finding a page data by ID
To find a specific page data by ID, you use the GetPageDataList method:
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.
Finding a page data by title
For 6.3 and below:
To find a specific page data by its title, first, you use the GetPageDataList method. Then you filter based on the Title property:
public
static
PageData FindPageByTitleNativeAPI(
string
title)
{
PageManager pageManager = PageManager.GetManager();
PageData page = pageManager.GetPageDataList().Where(pData => pData.Title == title).FirstOrDefault();
return
page;
}
For 7.0 and above:
PageData now has a NavigationNode property 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:
public
static
PageData FindPageByTitleNativeAPI(
string
title)
{
PageManager pageManager = PageManager.GetManager();
PageData page = pageManager.GetPageDataList().Where(pData => pData.NavigationNode.Title == title).FirstOrDefault();
return
page;
}
Finding a page data by URL of the 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:
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.Page;
}
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:
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:
public
IQueryable<PageData> GetLivePagesNativeAPI()
{
PageManager pageManager = PageManager.GetManager();
IQueryable<PageData> pages = pageManager.GetPageDataList().Where(pData => pData.Status == ContentLifecycleStatus.Live);
return
pages;
}
Finding a page 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.
Finding a 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:
public
PageNode FindPageNodeByIdFluenAPI(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.
Finding a 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:
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().Page;
}
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.
Finding a 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:
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;
}
Finding a 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:
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.
public
static
IQueryable<PageData> GetPagesByTemplateFluentAPI(Guid templateId)
{
return
App.WorkWith().Pages().Where(p => p.Page !=
null
&& p.Page.Template.Id == templateId && p.Page.Status == ContentLifecycleStatus.Live).Get().Select(p => p.Page);
}
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:
public
IQueryable<PageNode> GetLivePagesFluentAPI()
{
IQueryable<PageNode> pageNodes = App.WorkWith().Pages().Where(pN => (pN.Page !=
null
&& pN.Page.Status == ContentLifecycleStatus.Live)).Get();
return
pageNodes;
}
Get all translations of a page
Native API
To get all translations of a page, you use the GetPageNodes method. Then, you filter based on the UiCulture property, the LocalizationStrategy, Title and NodeType. Finally you can use the FilterPagesForLanguage method to get all pages for a specific language:
var cultureInfo = new System.Globalization.CultureInfo("cs");
var currentCulture = Thread.CurrentThread.CurrentUICulture;
var ci = System.Globalization.CultureInfo.GetCultureInfo("cs");
var pages = pageManager.GetPageNodes().Where(pageNode =>
(pageNode.Page.UiCulture == "cs"||
pageNode.Page.LocalizationStrategy != Telerik.Sitefinity.Localization.LocalizationStrategy.Split) && pageNode.Parent.Title == "LinksTest CS" &&
pageNode.NodeType == NodeType.Standard);
var filteredTranslations = PageHelper.FilterPagesForLanguage(pages, ci).AsQueryable();
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, Title and Page properties of the PageData object:
var childNodes = App.WorkWith()
.Pages()
.LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
.Where(p => p.Parent.Title ==
"LinksTest CS"
&& p.Page !=
null
&& p.NodeType==NodeType.Standard)
.ThatArePublished()
.ThatAreForLanguage(ci)
.Get();