More in this section
Forums / Designing with Sitefinity / DB script to change template on over 50 pages

DB script to change template on over 50 pages

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
6 posts, 0 answered
  1. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    26 Mar 2009
    Link to this post
    I've got to change the template on over 50 pages
    i either do this manually, or try and script it

    anyone got a script that can do this please?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    26 Mar 2009
    Link to this post
    Hello Quade,

    I suggest that you should use Sitefinity API.

    Here is a sample that will change template to all pages. You can get a certain one as well.

            CmsManager manager = new CmsManager(); 
            IList ListOfAllPages = manager.GetPages(); 
            foreach (IPage page in ListOfAllPages) 
            { 
                IPageTemplate pageTemplate = manager.GetTemplate("Default"); 
                page.Staged.SetTemplate(pageTemplate.ID, "ThemeName"); 
                manager.SavePage(page);                        
            } 

    I hope this helps.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    02 Apr 2009
    Link to this post
    Thanks Ivan

    Can it be done manually in the DB too?
    If so which tables am i looking at?

    thanks
  4. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    02 Apr 2009
    Link to this post
    sorry forgot to add

    i need to look for pages using a templateA, and change these pages to use templateB
  5. Meister
    Meister avatar
    262 posts
    Registered:
    09 Nov 2007
    02 Apr 2009
    Link to this post
    I've tried doing this

        Dim manager As CmsManager = New CmsManager()
        Dim ListOfAllPages As IList = manager.GetPages()
            For Each page As IPage In ListOfAllPages

                If page.Template.Name = "AAAAA" Then
        Dim pageTemplate As IPageTemplate = manager.GetTemplate("BBBBB")
                    page.Staged.SetTemplate(pageTemplate.ID,  "BBBBB")
                    manager.SavePage(page)

                End If

            Next page


    however, i'm getting the error:
    You cannot change a value of an object that is not in transaction.


    the stack trace is:
    "at Nolics.ORMapper.Base.OdbClassHelper.CheckAssign(IOdbView clView, String propertyName, Object oldValue, Object newValue)    at Telerik.Cms.Data.StagedPage.set_TemplateID(Guid value)    at Telerik.Cms.Data.StagedPage.SetTemplate(Guid templateId, String theme)    at Support_BulkSettings.btnChangeMasterPage_Click(Object sender, EventArgs e) in .....Support\BulkSettings.aspx.vb:line 19    at System.Web.UI.WebControls.Button.OnClick(EventArgs e)    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)"
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Apr 2009
    Link to this post
    Hello Quade,

      // instance of ContentManager 
            CmsManager manager = new CmsManager(); 
            // list of pages 
            IList ListOfAllPages = manager.GetPages(); 
            foreach (IPage page in ListOfAllPages) 
            { 
                if (page !=null && page.Template != null && page.Template.Name == "Default"
                { 
                    // get the page 
                    ICmsPage cmspage = manager.GetPage(page.ID, trueas ICmsPage; 
                    // get template 
                    IPageTemplate pageTemplate = manager.GetTemplate("MasterPage"); 
                    // set template 
                    cmspage.Staged.SetTemplate(pageTemplate.ID, "Blue with right sidebar"); 
                    // save page 
                    manager.SavePage(cmspage); 
                } 
            } 

    Sql query against db

    update sf_PageContentBase 
    set MasterPage='Default.master' 
    where MasterPage='MainMasterPage.master' 
     


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Register for webinar
6 posts, 0 answered