1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding a CmsWebControl

Adding a CmsWebControl

7 posts, 1 answered
  1. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    17 Feb 2010
    Link to this post
    Hi,

    I'm developing a tool that needs to add some controls as CmsWebControls to a page.

    My latest try was with this code, where I'm trying to add a GenericContent as a CmsWebControl

    1.Telerik.Cms.Engine.WebControls.GenericContent gc = new Telerik.Cms.Engine.WebControls.GenericContent();
    2. 
    3.gc.Content = "my content";
    4.CmsWebControl control = new CmsWebControl(gc);
    5.control.ID = Guid.NewGuid();
    6.control.Name = "controlName";
    7.content.Staged.AddControl("banner",control);
    8.content.Publish();

    I'm receiving a Null Reference Exception when I try to Publish. When I tried to add the GenericContent directly, everything worked but I need a way to add CmsWebControls.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Feb 2010
    Link to this post
    Hi Fernando,

    You should work with the interface ICmsWebControl. Most probably you need to get the page and then publish it. You can take a look at this KB article that shows how to add control on a page - How to create predefined template pages that contain controls placed on the content areas

    Greetings,
    Ivan Dimitrov
    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.
  3. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    17 Feb 2010
    Link to this post
    Hi Ivan,

    I've got the page using:

    CmsManager manager = new CmsManager();
    CmsPageContent content = manager.GetPage(new Guid("26B62D6C-E845-4386-B5E4-016473300B1F"), true);

    and then that code...

    but if I use the interface ICmsWebControl, how could I set the properties and attributes for the control?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Feb 2010
    Link to this post
    Hello Fernando,

    Cast the control you are looking for to its type - in your case GenericContent.

    All the best,
    Ivan Dimitrov
    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.
  5. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    17 Feb 2010
    Link to this post
    I can't cast it. When I cast, it results in null.

    I've used this to cast, what resulted in null and returned a Null Exception.

    1.CmsManager manager = new CmsManager();
    2.CmsPage content = manager.GetPage(new Guid("26B62D6C-E845-4386-B5E4-016473300B1F"), true) as CmsPage;
    3. 
    4.Telerik.Cms.Engine.WebControls.GenericContent gc = new Telerik.Cms.Engine.WebControls.GenericContent();
    5.  
    6.gc.Content = "my content";
    7.content.Staged.AddControl("banner", gc as ICmsWebControl);
    8.content.Publish();

    Is this what you mean?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Feb 2010
    Link to this post
    Hi Fernando,

    1. Add the control on the page.
    2. Then get the control and set its properties.

    foreach (ICmsWebControl cntrl in staged.Controls)
       {
          if (control.ControlType == (typeof(GenericContent)))
           {
               Control c = cntrl.LoadControl(this.Page);
               // or use GenericContent cnt = (GenericContent)control.LoadControl();
               GenericContent gcControl = c as GenericContent;
               if (gcControl != null)
               {
                   gcControl.Content = "some content"
               }
     
           }
       }


    All the best,
    Ivan Dimitrov
    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.
    Answered
  7. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    18 Feb 2010
    Link to this post
    Hi Ivan,

    This code sample has helped me a lot!

    The control.LoadControl() method was what solved my problem, because it returns a System.Web.UI.Control.

    Thanks!
Register for webinar
7 posts, 1 answered