Sitefinity CMS

Finding Pages Send comments on this topic.
See Also
Developing with Sitefinity > Pages > Pages API Walkthrough > Pages > Finding Pages

Glossary Item Box

This topic demonstrates how to use Sitefinity Pages API to find single or multiple pages. Apart from CmsManager class you can use SiteMap provider for this purposes (as demonstrated at the end of this topic), because Sitefinity implements native ASP.NET SiteMap provider.

 

There are several methods to use to find pages:

Get page by its ID – unique identifier:

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get the current node of sitemap
Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
// get the CmsPage represented by the current node and extract its ID
Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
Guid currentPageId = currentPage.ID;
// for demonstration purposes we are going to retrieve the IPage object by passing the
// id of the current object to the GetPage function
Telerik.Cms.IPage pageFromDb = cmsManager.GetPage(currentPageId);
// let's output the name of the page we have retrieved from the database
Response.Write(pageFromDb.Name);

 

Get page (for editing) by its ID – unique identifier:

Use this function if you wish to modify the page.

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get the current node of sitemap
Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
// get the CmsPage represented by the current node and extract its ID
Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
Guid currentPageId = currentPage.ID;
// for demonstration purposes we are going to retrieve the IPage object by passing the
// id of the current object to the GetPage function. For the second argument ("forEdit")
// we are going to pass true - which means we are able to modify this page
Telerik.Cms.IPage pageFromDb = cmsManager.GetPage(currentPageId, true);
// let's change the name of the page and save the page with CmsManager
pageFromDb.Name = "ChangedName";
cmsManager.SavePage(pageFromDb);
// let's output the name of the page we have retrieved from the database
Response.Write(pageFromDb.Name);

 

Get a child page:

Use this function when you know the parent page and the name of the child page.

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get the current node of sitemap
Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
// get the CmsPage represented by the current node and extract its ID
Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
Guid currentPageId = currentPage.ID;
// let's find the ChildPage1 page, which is a child of currentPage
Telerik.Cms.IPage childPage1 = cmsManager.GetChildPageByName(currentPageId, "ChildPage1");
// we can cast childPage1 as ICmsPage now and write its menu label
Response.Write(((Telerik.Cms.ICmsPage)childPage1).MenuName);

 

Get page by one of its additional URLs:

Remember that any page in Sitefinity can have multiple additional URLs.

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// let's get the page by passing one of it's additional url's
Telerik.Cms.IPage myPage = cmsManager.GetPageByAdditionalUrl("~/News/SomePageSecondaryUrl.aspx");
// we can cast myPage as ICmsPage and write out its default url and see that it's different
// from the additional url
Response.Write(((Telerik.Cms.ICmsPage)myPage).DefaultUrl.Url);

 

Get all pages of the Web site:

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get all pages
IList allPages = cmsManager.GetPages();
// write out the number of pages
Response.Write(allPages.Count.ToString());

 

Get all child pages of a parent page:

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get the current node of sitemap
Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
// get the CmsPage represented by the current node and extract its ID
Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
Guid currentPageId = currentPage.ID;
// get all child pages of currentPage
IList childPages = cmsManager.GetPages(currentPageId);
// write out the number of child pages
Response.Write(childPages.Count.ToString());

 

Getting references to pages using standard ASP.NET SiteMap provider (recommended way for reading pages):

Copy Code
// Sitefinity is using standard ASP.NET SiteMap provider. Therefore,
// Sitefinity pages can be accessed just as any ASP.NET website using
// SiteMap provider. It is important to note that SiteMapNode should be
// case to Telerik.Cms.Web.CmsSiteMapNode, which is our own improved version
// of SiteMapNode object which provides a very handy CmsPage property - a reference
// to the ICmsPage object associated with the particular node
Telerik.Cms.Web.CmsSiteMapNode siteNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.CurrentNode;
foreach (Telerik.Cms.Web.CmsSiteMapNode childNode in siteNode.ChildNodes)
{
 Response.Write(childNode.CmsPage.MenuName);
}
You can find more information about SiteMap Providers in the following MSDN topic.

 

See Also