More in this section

Forums / Developing with Sitefinity / how to add controls to a programatically created page

how to add controls to a programatically created page

4 posts, 0 answered
  1. viscious
    viscious avatar
    31 posts
    Registered:
    10 Aug 2006
    24 Jul 2007
    Link to this post
    I am developing a calendar intra site module.

    Functionally the calendar will be similar to how the blog module works. On the backend you can create multiple calendars.  You can select a calendar to work with, and then you can add events to the calendar.   Each event will have a name, date, location, etc.  But it will also have content associated with it, just like a blog post would.

    On the front end, there will be a main page for each calendar.  On this page, there will need to be a control that lists out the upcoming calendar events.   Just like a list of blog posts.   This page I will manually create, no problems here.

    However each event on the calendar will have a permalink associated with it.  On the main calendar page, if you click the title of the event, I want to navigate to that events perma link.  

    The problem is,  I don't want the end user to have to manually create a page for each event, then add a  "CalendarEvent" control onto it, and then select which event to bind that control to.

    What i want to do is this.   In the back end, everytime an event is added to a calendar, i want the calendar module to automatically create a page for that event, add the "CalendarEvent" control to that page, set the template and theme of that page.    How can this be accomplished?

    I have reviewed the developer manual, and have seen the code necessary to create a page, but I am not sure how to add the controls to the page, or set the theme, template, etc.




  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    25 Jul 2007
    Link to this post
    Hello Erick,

    You should get the staged version of ICmsPage object and use its methods and properties to add controls and set theme and template.
    Have a look at the example – userPage is the created ICmsPage object.

          Label lbl = new Label();
      lbl.ID = "Label1";

      lbl.Text = "My Label";

      IStagedPage stage = userPage.Staged.CheckOut();

      // "Content" is the id of the container in the Master page
      stage.AddControl("Content", lbl);
      stage.Template = manager.GetTemplate("default");
      stage.Theme = "Blue with right sidebar";

         stage.CheckIn();

    Hope this helps.

    All the best,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. viscious
    viscious avatar
    31 posts
    Registered:
    10 Aug 2006
    25 Jul 2007
    Link to this post
    Thanks for the reply Pepi.  Unfortunately I am getting an exception when i try that code.


    The exception is:

    userpage.Staged.Template = manager.GetTemplate( "Main Template" );


    Unable to cast object of type 'Telerik.Cms.Data.CmsTemplateContent' to type 'Telerik.Cms.Data.PageTemplate'. 


    I have to say, I am a bit disappointed in the development documentation thus far.   Is there any plans to better document this part of the API?  I had expected this type of information to be in the developer manual, but it doesn't seem to be very complete.  

    The api reference section is nothing more than a listing of classes and there is no information on how to use them.  And as far as intellisense goes  none of the methods, classes, interfaces, or paramaters have any sort of xml comments on them.  Very frustrating to try to figure this stuff out by trial and error.

    One thing that would be of great help would be to get the source of a module that is more complete than the jobs one.  The jobs one is a good way to get started, but it lacks the features of a more complete module.   It would be great to see how things are accomplished in a module such as blogs or such.

    Or at the very least, modify the license to allow the use of Reflector etc.

    Anyways.  I don't mean to complain this much, the overall product has been a pleasant experience.  I just wish the developer documentation was more thorough

  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    26 Jul 2007
    Link to this post
    Hello Erick,

    You are right that the API should be thoroughly documented and I can assure you that we are working hard on this and we'll do our best to make it much more comprehensive and useful. Your feedback on this matter is highly appreciated.

    As for the dynamically created page, you could try the following code:

    private void CreateUserPage(string userName, string firstName, out Guid pageId)  
    {  
         // creates user's page  
         CmsManager manager = new CmsManager();  
         ICmsPage userPage = manager.CreatePage(userName);  
         pageId = userPage.ID;  
         userPage.Navigable = true;  
         userPage.MenuName = firstName;  
     
         manager.SavePage(userPage);  
     
         userPage = (ICmsPage)manager.GetPage(userPage.ID, true);  
     
         Label lbl = new Label();  
         lbl.ID = "Label1";  
         lbl.Text = "My Label";  
     
        IStagedPage stage = userPage.Staged.CheckOut();  
     
         // adds Label control to the page in a container with ID Content  
         stage.AddControl("Content", lbl);  
     
         userPage = (ICmsPage)manager.GetPage(userPage.ID, true);  
     
        // gets template with a name "default" and after that sets it for the newly created page  
         IPageTemplate template = manager.GetTemplate("default");  
         userPage.Staged.SetTemplate(template.ID, "Blue with right sidebar");  
     
         userPage = (ICmsPage)manager.GetPage(userPage.ID, true);  
         userPage.Stage.CheckIn();  
    }  
     

    Please note that ICmsPage object needs to be retrieved after each operation because the methods AddControl and SetTemplate commit the current transaction automatically. Although this does not affect performance as the object is retrieved from the cache we will make overloads to handle bulk operations.

    Thank you for drawing our attention to this.

    Please let me know how it goes and don’t hesitate to ask if you have any further questions.


    Best wishes,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Register for webinar
4 posts, 0 answered