Sitefinity CMS

Working with Pages Programmatically Send comments on this topic.
See Also
Developing with Sitefinity > Pages > Working with Pages Programmatically

Glossary Item Box

Sitefinity provides a user-friendly way to create pages in the Administration section. This is done by setting some settings for a new page such as title and page type, and then choosing a template and a theme for that page. Another possibility to create pages is to add them programmatically. The following example demonstrates how to create a page for a single user profile in a Dating site. 

 

Creating a Page

First, there should be an instance of the CmsManager object. It provides means to control the Sitefinity pages.  An instance of the CmsManager is created and then the CreatePage(string pageName) method is used to create the page. Once the page is created, its properties (Navigable, Menu name, and so on) could also be set.

[C#] Create user page

Copy Code
private void CreateUserPage(UserSex userSex, string userName, string firstName, out Guid pageId)
{
 
// create user's page
 
CmsManager manager = new CmsManager();
 ICmsPage userPage = manager.CreatePage(userName);
 pageId = userPage.ID;
 userPage.Navigable = true;
 userPage.MenuName = firstName;
      
 
// depending on the user's sex set parent page and appropriate template
 
if (userSex == UserSex.Man)
 {
   userPage.ParentID = menPageId;
   userPage.Staged.Template = manager.GetTemplate(
"Man user profile");
 }
 
else
   
userPage.ParentID = womenPageId;
 userPage.Staged.Template = manager.GetTemplate(
"Woman user profile");
           
 userPage.Publish();
}

 

Getting Pages 

Similarly to creating pages programmatically in Sitefinity, an instance of the CmsManager is needed also when you need to get a page. Pages could be accessed either by their id (GUID) or a collection of pages having the same parent.

 

The example below demonstrates how to get a page in the ItemDataBound event of RadGrid. The page object is then used to create a link to that page in this specific grid item:

Getting a Page Copy Code
protected void gridProfiles_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
{
 
if (e.Item is Telerik.WebControls.GridDataItem)
 {
   HyperLink linkProfile = (HyperLink)e.Item.FindControl(
"linkProfile");
   Guid pageId =
new Guid(((Telerik.Samples.Dating.Data.UserProfile)e.Item.DataItem).PageId.ToString());
   CmsManager manager =
new CmsManager();
   ICmsPage profilePage = (ICmsPage)manager.GetPage(pageId);
   
if (profilePage != null)
   {
     
string profileUrl = profilePage.DefaultUrl.Url;
     linkProfile.NavigateUrl = profileUrl;
   }
 }
}

 

See Also