More in this section

Forums / Developing with Sitefinity / Prepopulating a template with generic controls

Prepopulating a template with generic controls

3 posts, 0 answered
  1. Richard
    Richard avatar
    77 posts
    Registered:
    24 Oct 2007
    04 Feb 2010
    Link to this post
    Hi!
    My first question here, so please forgive me for such silly questions.  I have attempted to search for this problem in the forum and have found something that only partially answers it.  Anyway, I will start with my issue firstly:

    I am trying to "simply" prepopulate pages with a few Generic Content (GC) controls.  I have tried to achieve this by adding GC controls to the template page, saving this and then creating a new page and trying (here is where it fails) editing it.  What happens is, is that there no GC appears for me to edit!

    After reading this :
    http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/set-generic-content-as-default-control.aspx

    Is it true that I cannot add GC controls and allow for users to edit content within the page Edit mode?

    If so, Is there anyway around this ?  It would be a convenience to start pages with default GC's in place, especially when 99% of those placeholders will be used for simply typing text.

    Thank you
    Richard

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    04 Feb 2010
    Link to this post
    Hi Richard Leung,

    Take a look at the following KB which shows how to add control on a page - How to create predefined template pages that contain controls placed on the content areas

    Is it true that I cannot add GC controls and allow for users to edit content within the page Edit mode?


    You cannot manage control properties only in case the control is hardcoded in the physical master page.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. Richard
    Richard avatar
    77 posts
    Registered:
    24 Oct 2007
    05 Feb 2010
    Link to this post
    Perfect, thank you Mr Dimitrov.

    For anyone else that happens to have such an issue and has read through the KB article, this might be worth noting:
    - You need to "getPage" before adding a control, and "savePage" after adding a control!
    - NAME_OF_TEMPLATE_ON_WEBSITE_HERE in the example below should be replaced by the name you find when you select the template in the website and not masterpage (if I am not mistaken!)
    - ContentPlaceholder1  in the example below refers to the ID of the control you added to your master page.
    - NAME_YOUR_NEW_CONTROL_GOES_HERE_1 in the example below should be replaced to any custom runtime ID you desire.

    01.using Telerik.Cms;
    02.using Telerik;
    03.using Telerik.Cms.Data;
    04.using Telerik.Cms.Engine.WebControls;
    05.using System.Collections;
    06.using System;
    07.  
    08.namespace Samples
    09.{
    10.    public class PageInitializer
    11.    {
    12.        public void PageActionExecuted(object sender, ExecutedEventArgs e)
    13.        {
    14.            if (e.CommandName == "CreatePage")
    15.            {
    16.                ICmsPage cmsPage = e.Data as ICmsPage;
    17.                if (cmsPage.Staged != null)
    18.                    InitializePage(cmsPage);
    19.            }
    20.        }
    21.  
    22.        public void InitializePage(ICmsPage cmsPage)
    23.        {
    24.            // create a new instance of the CmsManager    
    25.            CmsManager cmsManager = new CmsManager();
    26.  
    27.            // get the page associated with transaction 
    28.            IPage page = cmsManager.GetPage(cmsPage.ID, true);
    29.  
    30.            // get the template that the page should use 
    31.            IPageTemplate SavepageTemplate = cmsManager.GetTemplate("NAME_OF_TEMPLATE_ON_WEBSITE_HERE");
    32.  
    33.            // assign the template to the page and optionally the theme 
    34.            page.Staged.SetTemplate(SavepageTemplate.ID, string.Empty);
    35.  
    36.            // add the GenericContent control to "Content" placeholder 
    37.            page.Staged.AddControl("ContentPlaceHolder1" , typeof(GenericContent), 0,"NAME_YOUR_NEW_CONTROL_GOES_HERE_1");
    38.            cmsManager.SavePage(page);
    39.  
    40.            page = cmsManager.GetPage(cmsPage.ID, true);
    41.            // add the GenericContent control to "Content" placeholder 
    42.            page.Staged.AddControl("ContentPlaceHolder2", typeof(GenericContent),  0, "NAME_YOUR_NEW_CONTROL_GOES_HERE_2");
    43.            cmsManager.SavePage(page);
    44.  
    45.            page = cmsManager.GetPage(cmsPage.ID, true);
    46.            // add the GenericContent control to "Content" placeholder 
    47.            page.Staged.AddControl("ContentPlaceHolder3", typeof(GenericContent), 0, "NAME_YOUR_NEW_CONTROL_GOES_HERE_3");
    48.            cmsManager.SavePage(page); 
    49.  
    50.        }
    51.    }
    52.}

Register for webinar
3 posts, 0 answered