More in this section

Forums / Developing with Sitefinity / Trouble programmatically creating a page.

Trouble programmatically creating a page.

2 posts, 0 answered
  1. Christian Ecker
    Christian  Ecker avatar
    7 posts
    08 Mar 2010
    04 Aug 2010
    Link to this post
    I am trying to programmatically create a page, set it to use an existing template and theme, and then add controls to the containers. Based on the documentation I've come up with the code below:

    01.CmsManager cmsManager = new CmsManager();
    02.ICmsPage p = cmsManager.CreatePage(this.subjectBox.Text);
    03.IStagedPage stagedPage;
    04.GenericContent gc0, gc1;
    06.p.Staged.Template = cmsManager.GetTemplate("EnvisionMain");
    07.p.Staged.Theme = "Envision";
    08.gc0 = new GenericContent();
    09.gc0.Content = this.desc0.Content;
    10.gc1 = new GenericContent();
    11.gc1.Content = this.desc1.Content;
    12.p.Staged.AddControl("CuratedContent", gc0);
    13.p.Staged.AddControl("CuratedContent", gc1);

    However this fails with the error "Unable to cast object of type 'Telerik.Cms.Data.CmsTemplateContent' to type 'Telerik.Cms.Data.PageTemplate'." at line 6. Can someone provide me with guidance for the best way to proceed?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    01 Feb 2016
    05 Aug 2010
    Link to this post
    Hi Christian Ecker,

    Thank you for using our services.

    Please examine this sample code. It illustrates how to achieve what you are looking for:
    using System;
    using Telerik.Cms;
    using Telerik.Cms.Engine.WebControls;
    public partial class test : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            CmsManager manager = new CmsManager();
            ICmsPage page = manager.CreatePage("TestPage");
            page.MenuName = "Test Page";
            page.Description = "This is my page description.";
            page.Navigable = true;
            page.Keywords = "Page,Keywords";
            page.DenyAnonymous = false;
            IStagedPage staged = page.Staged.CheckOut();
            IPageTemplate template = manager.GetTemplate("Inner");
            staged.SetTemplate(template.ID, "Emerald-Blue");
            GenericContent control1 = new GenericContent();
            control1.ID = "genericContent1";
            control1.Content = "<p>This is some content #1.</p>";
            GenericContent control2 = new GenericContent();
            control2.ID = "genericContent2";
            control2.Content = "<p>This is some content #2.</p>";
            // Get Page #1
            page = (ICmsPage)manager.GetPage(page.ID, true);
            page.Staged.AddControl("Content", control1);
            // Get Page #2
            page = (ICmsPage)manager.GetPage(page.ID, true);
            page.Staged.AddControl("Content", control2);
            // Get Page #3
            page = (ICmsPage)manager.GetPage(page.ID, true);

    This code is taken from the Sitefinity API Tips & Tricks webinar. You can take a look at the webinar for more information on how to work with Pages API.

    Best wishes,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered