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

Forums / Developing with Sitefinity / Programatically changing page theme

Programatically changing page theme

12 posts, 1 answered
  1. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    17 Dec 2010
    Link to this post

    I'm trying to create a method in the Sitefinity admin (we are running 3.7) to change the theme for every child page located in a page group.  I'm having trouble with the publish part of it.  When I run it, it only modifies the theme of the first record and will only run 4-5 times before stopping.  When it stops, it doesn't error and it doesn't end successfully.  Here'e the code I'm using - please let me know where I'm missing it.

    01.protected void LinkButton_Click(object sender, EventArgs e)
    02.{
    03.    SiteMapPanel siteMapPanel = (SiteMapPanel)this.Parent.Parent.Parent;
    04.    Guid selectedPageID = siteMapPanel.SelectedPageID;
    05.      
    06.    Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
    07.    IList childPages = cmsManager.GetPages(selectedPageID);
    08.    foreach (CmsPage row in childPages) 
    09.    {
    10.        Guid currentPageId = row.ID;
    11.        Telerik.Cms.IPage pageFromDb = cmsManager.GetPage(currentPageId, true);
    12.        pageFromDb.Staged.Theme = "New_Theme";
    13.        row.Publish();
    14.    }
    15.    Response.Write("Done.");  
    16.}
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Dec 2010
    Link to this post
    Hi Randy,

    This is not the correct way to do this. The method you use is very heavy an you commit the page into nolics transaction which alters the database tables. You can change the theme at runtime by creating a custom class that inherits from InternalPage and overriding GetTheme method. You can take a look at this post - Sitefinity & Accessibility

    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. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    20 Dec 2010
    Link to this post
    Ivan,

    Thank you for your response.

    Our admin users have the ability to copy pagegroups.  We have a few themes that we'd like them to be able to apply to a newly copied page group.  Right now they have to go into 30 - 40 pages to change the theme over.  I saw the link for doing it at runtime but I'd like to avoid it if possible for two reasons:  I dont want the users (non admin) to be able to change or see the theme and I don't want to pass the querystring around for design mode.  Is there a way to globally update the page theme in the database for every page in a page group? 
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Dec 2010
    Link to this post
    Hi Randy,

    You can use the pages API and its method SetTemplate method of StagedPage object

    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
  5. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    20 Dec 2010
    Link to this post
    Ivan,

    Thanks again.  I'm still having trouble - do you have an example of how I can do this using the pages API?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Dec 2010
    Link to this post
    Hi Randy,

    Below is a sample code

    CmsManager cmsManager = new CmsManager();
     
    // get the page associated with transaction
    IPage page = cmsManager.GetPage(cmsPage.ID, true);
     
    // get the template that the page should use
    IPageTemplate pageTemplate = cmsManager.GetTemplate("Default");
     
    // assign the template to the page and optionally the theme
    page.Staged.SetTemplate(pageTemplate.ID, string.Empty);
     
    // add the GenericContent control to "Content" placeholder
     
     
    cmsManager.SavePage(page);


    Regards,
    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
  7. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    20 Dec 2010
    Link to this post
    Here's what I've got:
    01.SiteMapPanel siteMapPanel = (SiteMapPanel)this.Parent.Parent.Parent;
    02.       Guid selectedPageID = siteMapPanel.SelectedPageID;
    03.         
    04.       Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
    05.       IList childPages = cmsManager.GetPages(selectedPageID);
    06.       foreach (ICmsPage cmsPage in childPages) 
    07.       {
    08.           // get the page associated with transaction 
    09.           IPage page = cmsManager.GetPage(cmsPage.ID, true);
    10.           // get the template that the page should use 
    11.           IPageTemplate pageTemplate = cmsManager.GetTemplate(page.Template.Name);
    12.           // assign the template to the page and optionally the theme 
    13.           page.Staged.SetTemplate(pageTemplate.ID, "New_Theme");
    14.           // add the GenericContent control to "Content" placeholder 
    15.           cmsManager.SavePage(page);
    16.        }

    I'm getting an error when it tries to execute line 15
    The error is :
    {"no segments* file found: files:":null}
       at Telerik.Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean create, Boolean closeDir)
       at Telerik.Lucene.Net.Index.IndexWriter..ctor(String path, Analyzer a, Boolean create)
       at Telerik.Search.Engine.Crawler.GetIndexWriter(String indexName, Boolean createIndex)
       at Telerik.Search.Engine.BaseIndexer..ctor(String directory, String provider, Boolean appendToIndex)
       at Telerik.Search.Engine.IndexingManager.DeleteDocumentsFromIndex(String provider, Guid[] documentIds)
       at Telerik.Search.Engine.IndexingService.Client_Index(Object sender, IndexEventArgs e)
       at Telerik.Cms.Search.PageIndexProvider.OnIndex(IndexEventArgs args)
       at Telerik.Cms.Search.PageIndexProvider.CmsManager_Executed(Object sender, ExecutedEventArgs e)
       at Telerik.ManagerBase`1.OnExecuted(Object sender, ExecutedEventArgs args)
       at Telerik.Cms.Data.DefaultProvider.OnExecuted(ExecutedEventArgs args)
       at Telerik.Cms.Data.DefaultProvider.SavePage(IPage page)
       at Telerik.Cms.CmsManager.SavePage(IPage page)
       at Sitefinity_UserControls_PageGroupThemeChanger_PageGroupThemeChanger.LinkButton_Click(Object sender, EventArgs e) in c:\etfs.ntl-city.com\MortgageWeb\Source\Trunk\PNC.CM.MW\PNC.CM.MW.Web\Sitefinity\UserControls\PageGroupThemeChanger\PageGroupThemeChanger.ascx.cs:line 58
       at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.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)

    What am I missing?
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Dec 2010
    Link to this post
    Hi Randy,

    Your page index is corrupted. You need to recreate it

    1. Go to Administration >> Services >> Search and delete the page index you have there
    2. Open App_Data/Search and check whether there is a folder with the same name as the index you have already removed. If there is such a folder remove it.
    3. Go to Administration >> Services >> Search and recreate your index.

    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
    Answered
  9. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    21 Dec 2010
    Link to this post
    Ivan,

    That was it!  Thanks for all your help - couldn't have done it without you.  Here's the final version of the code:

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        // Gets the Selected page id
        SiteMapPanel siteMapPanel = (SiteMapPanel)this.Parent.Parent.Parent;
        Guid selectedPageID = siteMapPanel.SelectedPageID;
        // Pass page id to theme changer
        ChangePageTheme(selectedPageID);
    }
    protected void ChangePageTheme (Guid parentPage) {
        Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
        // Gets all child pages of page
        IList childPages = cmsManager.GetPages(parentPage);
        foreach (ICmsPage cmsPage in childPages)
        {
            // Get the page associated with transaction 
            IPage page = cmsManager.GetPage(cmsPage.ID, true);
            // If current page is a page group or if current page contains pages pass page id
            // to ChangePagetheme to alter child pages.  This ensures that every branch in the 
            // tree is changed
            if (cmsPage.Pages.Count > 0 || cmsPage.PageType == CmsPageType.Group)
            {
                ChangePageTheme(page.ID);
            }
            else
            {
                // Use the existing template of the page - we don't want to change this 
                IPageTemplate pageTemplate = cmsManager.GetTemplate(page.Template.Name);
                // Assign the template to the page (original) and the theme 
                page.Staged.SetTemplate(pageTemplate.ID, "New_Theme");
                //Save the page
                cmsManager.SavePage(page);
            }
        }
    }

    Thanks again,
    Randy
  10. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    04 Jan 2011
    Link to this post
    Ivan,

    One last problem.  When I go back to the site, all the pages have the correct theme but they all show that I have the page locked - how do I fix that?
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    04 Jan 2011
    Link to this post
    Hi Randy,

    Try to check out the page before the foreach loop staged = page.Staged.CheckOut(); and after you change the theme  execute CheckIn().

    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
  12. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    04 Jan 2011
    Link to this post
    Perfect - thanks.
Register for webinar
12 posts, 1 answered