More in this section

Forums / Developing with Sitefinity / Programmatically add my custom module to a page

Programmatically add my custom module to a page

2 posts, 0 answered
  1. Jenn Bohm
    Jenn Bohm avatar
    17 posts
    Registered:
    19 Aug 2009
    23 Mar 2010
    Link to this post
    I've created a module, and I'd like to add it to pages programmatically. I found the example of adding Generic Content modules to a page in the API Tips & Tricks webinar, and attempted to follow it. Here's what I'm trying to do:

    01.mgr_c = new CmsManager();
    02.  
    03.// create the page under the selected category
    04.ICmsPage categoryPageGroup = GetPageByUrl("/" 
    05.   + wslrDomainName 
    06.   + "/" + ddlCategory.SelectedValue);
    07.ICmsPage newPage = mgr_c.CreatePage(Server.HtmlEncode(txtRetailerPageName.Text), categoryPageGroup.ID);
    08.  
    09.// set the page properties
    10.newPage.Navigable = true;
    11.newPage.DenyAnonymous = true;
    12.newPage.Title = Server.HtmlEncode(txtPageTitle.Text);
    13.newPage.MenuName = Server.HtmlEncode(txtLinkText.Text);
    14.  
    15.// set the template
    16.IPageTemplate retailerPageTemplate = mgr_c.GetTemplate("RetailerPage");
    17.newPage.Staged.SetTemplate(retailerPageTemplate.ID, "ThemeName");
    18.  
    19.// add the RetailerDocuments control to the page
    20.ICmsWebControl retailerDocs = (ICmsWebControl)(new RetailerDocumentsModule());
    21.newPage.Staged.AddControl("ContentPlaceHolder0", retailerDocs);
    22.  
    23.// give permissions to proper retailer
    24.string retailer = ddlRetailers.SelectedItem.Text;
    25.string[] roles = { wslrID + "Retailer_" + retailer.Substring(7) };
    26.mgr_c.SecurityManager.SetPermissions((ISecured)newPage, 0, 1, roles);
    27.  
    28.// save and publish the page
    29.mgr_c.SavePage(newPage);
    30.newPage.Publish();

    I get an exception trying to cast my module as an ICmsWebControl, but AddControl seems to need an ICmsWebControl. I expect this is because I would have to implement the ICmsWebControl interface in my module in order to cast my control as one. Is there any way I can programmatically add this module to a page without having to do all this? I am able to add this module to a page manually, but I need to be able to do it programmatically.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 Mar 2010
    Link to this post
    Hi Jenn Bohm,

    You have created a module control, not a module. The modules resides in the backend. The modules has public controls that are used to show the module data on the front end.  Here is an useful article that describes this Sitefinity Architecture. Anyhow, you do not have to cast your control to ICmsWebControl
    You could use the following way to add the control

    page.Staged.AddControl("Content", typeof(MyControlType), 0, "ControlID1");
    cmsManager.SavePage(page);


    Best wishes,
    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.
2 posts, 0 answered