Sitefinity CMS

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

Glossary Item Box

In this topic we are going to demonstrate how to modify pages. First we are going to see how to modify a page when we think of page as part of a SiteMap or a Web site. Then we are going to see how to modify the page in its own context – how to change its appearance, function, and so on.

 

Modify page's properties and its characteristics (use ICmsPage interface for this purpose):

When you need to modify the page as a part of SiteMap you should use ICmsPage interface. In the following example we are going to move the page (change its parent) and change the page’s menu name.

Copy Code
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// get the node of sitemap that represents the page we will modify
Telerik.Cms.Web.CmsSiteMapNode nodeForEdit = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.FindSiteMapNode("~/Test1/ChangedName/ChildPage1.aspx");
// because we need page associated with transaction, we'll get the page from CmsManager and cast it to ICmsPage
Telerik.Cms.ICmsPage pageForEdit = cmsManager.GetPage(nodeForEdit.CmsPage.ID, true) as Telerik.Cms.ICmsPage;
// once we have page associated with transaction we are going to change its MenuName property
pageForEdit.MenuName = "Edited page";
// we are also going to move page to a new parent. To do this, we first need to get the
// id of the new parent page
Telerik.Cms.Web.CmsSiteMapNode newParentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.FindSiteMapNode("~/Test1/News.aspx");
// we'll set ParentID property to the id of the new parent page
pageForEdit.ParentID = newParentNode.CmsPage.ID;
// finally we are going to publish the page
pageForEdit.Publish();

 

Modify the page’s appearance and functionality (use IStagedPage interface for this):

When we want to change page’s appearance (change the theme or template) or to modify its functionality by adding, removing or moving its controls, we use IStagedPage interface. IPage object has a Staged property which is a reference to the staged version of the page (IStagedPaged type of object). In the following example we are going to programmatically change the theme of the current page and add new Generic Content control to one of its content place holders.

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);
// NOTE: to change the appearance and function of the page, we are going to use IStagedPage
// object. IPage object contains Staged property of type IStagedPage, which will let us
// to change the theme or add new control to the page for example.
// first we are going to change the theme of the page
pageFromDb.Staged.Theme = "White fluid";
// lets add a new GenericContent control and add it to the SideBarContent
// content place holder
Telerik.Cms.Engine.WebControls.GenericContent gcHello = new Telerik.Cms.Engine.WebControls.GenericContent();
gcHello.Content =
"<strong>Hello world!</strong>";
pageFromDb.Staged.AddControl(
"SideBarContent", gcHello);
// we need to publish current page after the changes we have made
currentNode.CmsPage.Publish();
// finally, because we are modifying the page we are currently viewing we need to refresh the page
// to see the changes
Response.Redirect(currentNode.Url);

See Also