Sitefinity CMS

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

Glossary Item Box

This topic will demonstrate various ways to programmatically find and access Sitefinity controls.

 

When talking about controls in this context, we mean the actual instance of these controls. This means that the control is of ICmsWebControl type.

For example, the BlogPosts control is not ICmsWebControl, but an instance of the BlogPosts control dropped on the Blogs.aspx page is an ICmsWebControl. In this topic we show how to find the actual instances of controls.

Controls are the building parts of Sitefinity pages. We distinguish two main ways of finding controls in Sitefinity:

  • Finding controls in a Web site (regardless of pages)
  • Finding controls for a particular page

 

Find all controls of a certain type (on all pages):

GetControls(string typeName, bool isAscx) Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// we can get all BlogPosts controls by passing the type of control we are looking for
// to CmsManager.GetControls function. The second argument determines if the controls
// we are looking for are UserControls or not
IList allBlogPostsControls = cmsManager.GetControls(typeof(Telerik.Blogs.WebControls.BlogPosts).ToString(), false);
// let's write out the number of BlogPosts controls used throughout the site
Response.Write(allBlogPostsControls.Count.ToString());

 

Find all controls on the given page:

Copy Code
// find the sitemap node of the current page
Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode;
// get the CmsPage associated with the current node
Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage;
// we are going to use Controls collection of a staged version of a page to
// access all controls of current page
IList<Telerik.Cms.ICmsWebControl> pageControls = currentPage.Staged.Controls;
foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls)
{
 
// let's write out some basic info on each control of this page
 
string ctrlInfo = String.Concat("Control ID = ", webCtrl.ID,
                                 
", Control type = ", webCtrl.ControlType,
                                 
", Container ID = ", webCtrl.ContainerID,
                                 
", Is UserControl = ", webCtrl.IsAscx);
 Response.Write(ctrlInfo +
"<br />");
}

 

See Also