In this topic we are going to demonstrate how to modify already existing controls on Sitefinity pages.
The following example will clearly demonstrate the difference between ICmsWebControl object and the actually functional control inside of it. Every
control that is added to a Sitefinity page is of type ICmsWebControl, but this interface is only a wrapper for the actual control that was dragged (such as
BlogPosts, GenericContent and so on).
How to modify the content of all GenericContent controls inside of a single container:
| SaveControl(ICmsWebControl control) |
Copy Code |
|
// create a new instance of CmsManager Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager(); // 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; // to each Generic Content control in SideBarContent we are going to append "[Obsolete]"
text foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls)
{
if (webCtrl.ContainerID == "SideBarContent" && webCtrl.ControlType == typeof(Telerik.Cms.Engine.WebControls.GenericContent))
{
// the following line clearly demonstrates the difference between ICmsWebControl (any control that
exists on the Sitefinity page)
// and the actual control this ICmsWebControl carries. We have checked if ControlType of webCtrl
is GenericContent, but to get
// the actual instance of GenericContent control, we need to call the LoadControl() function of
ICmsWebControl. You can think
// of ICmsWebControl objects as Sitefinity wrapper for the actual control
Telerik.Cms.Engine.WebControls.GenericContent gcCtrl = webCtrl.LoadControl() as Telerik.Cms.Engine.WebControls.GenericContent;
gcCtrl.Content = String.Concat("[Obsolete] ", gcCtrl.Content);
cmsManager.SaveControl(webCtrl);
}
} // finally we need to publish the current page and refresh it to see the results currentPage.Publish();
Response.Redirect(currentNode.Url);
|
See Also