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

Forums / Developing with Sitefinity / Programmatically setting a control template

Programmatically setting a control template

3 posts, 1 answered
  1. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    11 Oct 2010
    Link to this post
    Hi,

    I have a bunch of nearly identical pages with DownloadList controls.  I need to set the ItemListTemplatePath for these controls programmatically.  I have used the following code to set other attributes (filter expression) successfully, however when I try to run this to set the control template it doesn't seem to work.
    Any help would be greatly appreciated.

    public partial class FixProductLit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CmsManager cms = new CmsManager();
            IList pages = cms.GetPages();
     
            foreach (ICmsPage page in pages)
            {
                if (page.Name == "ProductLiterature" || page.Name == "News")
                {
                    ICmsPage parent = (ICmsPage)cms.GetPage(page.ParentID);
                    if (parent.MenuName != "COMPANY")
                    {
                        Response.Write("Fixing " + parent.MenuName + "...<br/>");
     
                        ICmsPage editPage = (ICmsPage)cms.GetPage(page.ID, true);
                        IStagedPage staged = editPage.Staged.CheckOut();
                        IList<ICmsWebControl> pageControls = editPage.Staged.Controls;
     
                        foreach (ICmsWebControl control in pageControls)
                        {
                            if (control.ControlType == typeof(DownloadList))
                            {
                                DownloadList dl = (DownloadList)control.LoadControl();
                                dl.ItemListTemplatePath = "~/Sitefinity/ControlTemplates/Libraries/DownloadListProductPage.ascx";
                                control.SetControl(dl);
                                cms.SaveControl(control);
                            }
                        }
     
                        editPage = (ICmsPage)cms.GetPage(page.ID, true);
                        editPage.Staged.CheckIn();
                        editPage.Publish();
                    }
                }
            }
            Response.Write("Done!<br/>");
        }
    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Oct 2010
    Link to this post
    Hi Keith,

    The code looks fine. Most probably it fails somewhere between the loops. You could attach it to Visual Studio debugger to see whether there is some fail. I performed a sample test and the property is correctly set. You also have to remove control.SetControl(dl);

    my code

    CmsManager cms = new CmsManager();
    ICmsUrlContext urlContext = CmsUrlContext.Current;
    ICmsPage parent = (ICmsPage)cms.GetPage(urlContext.PageID);
        IList<ICmsWebControl> pageControls = parent.Staged.Controls;
     
        foreach (ICmsWebControl control in pageControls)
        {
            if (control.ControlType == typeof(DownloadList))
            {
                DownloadList dl = (DownloadList)control.LoadControl();
                dl.ItemListTemplatePath = "~/Sitefinity/ControlTemplates/Libraries/DownloadListProductPage.ascx";
                cms.SaveControl(control);
            }
        }
     
        parent = (ICmsPage)cms.GetPage(urlContext.PageID, true);
        parent.Staged.CheckIn();
        parent.Publish();

    you could get the page in transaction

    CmsManager cms = new CmsManager();
                    ICmsUrlContext urlContext = CmsUrlContext.Current;
                    ICmsPage parent = (ICmsPage)cms.GetPage(urlContext.PageID, true);
                         
                        IStagedPage staged = parent.Staged.CheckOut();
                        foreach (ICmsWebControl control in staged.Controls)
                        {
                            if (control.ControlType == typeof(DownloadList))
                            {
                                DownloadList dl = (DownloadList)control.LoadControl();
                                dl.ItemListTemplatePath = "~/Sitefinity/ControlTemplates/Libraries/DownloadListProductPage.ascx";
                                cms.SaveControl(control);
                            }
                        }
      
                        parent = (ICmsPage)cms.GetPage(urlContext.PageID, true);
                        parent.Staged.CheckIn();
                        parent.Publish();

    and set the property.

    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
    Answered
  3. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    11 Oct 2010
    Link to this post
    Removing control.Setcontrol seemed to work...thanks!
Register for webinar
3 posts, 1 answered