Programmatically switch CS theme for main page and blogs

rated by 0 users
This post has 11 Replies | 2 Followers

Not Ranked
Posts 2
Points 40
c0de Posted: Fri, Apr 25 2008 7:53 AM

Hey,

I would like to switch CS themes programatically depending on context. Could you please suggest how to achieve this? I'm trying to play around user.Theme and Weblog.Theme but it doen't seem to solve the problem. Please find module code below

Thank you,

Anton

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Xml;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Misc
{
        internal enum ThemeTypeEnum
        {
                SampleTheme1
                ,SampleTheme2
        }

        public class ThemeSwitcher:ICSModule
        {
                      

#region ICSModule Members

                public void Init(CSApplication csa, XmlNode node)
                {
                        csa.UserKnown += csa_UserKnown;
                }

                private void csa_UserKnown(User user, CSEventArgs e)
                {

                        ChangeThemes(user);
                       
                }

                private void ChangeThemes(User currentUser)
                {
                        if (currentUser != null)
                        {
                          
                                String themeParameter = HttpContext.Current.Request.QueryString["th"];
                                if (!String.IsNullOrEmpty(themeParameter))
                                {
                                        ThemeTypeEnum themeType = (ThemeTypeEnum) Enum.Parse(typeof (ThemeTypeEnum), themeParameter);

                                        currentUser.Theme = themeType.ToString("g");
                                        CSContext currentContext = CSContext.Current;

                                        currentContext.SiteSettings.DefaultTheme = themeType.ToString("g");
                                        List <Section> weblogsList = Weblogs.GetWeblogs(true, false, true);
                                        foreach (Section section in weblogsList)
                                        {
                                                Weblogs.GetWeblog(section.ApplicationKey).Theme = themeType.ToString("g");
                                        }
                                }
                        }
                }
        }
}

Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers
Ben Tiedt replied on Sat, Apr 26 2008 8:01 AM

CS2008 includes changes to better support this scenario by exposting the CSContext.SiteTheme and CSContext.SectionTheme properties that can be set via an HTTP Module during the AuthenticateRequest event.

You can use a similar approach and set either the user.Theme or weblog.Theme property in CS2007, but there is one catch: You'll be changing this setting for all users, so, when setting user.Theme when the user is anonymous (or if your context could be different for the same user viewing two pages), you'll need to clone the CSContext.User object and reset that property before setting the CSContext.User.Theme property.

Ben Tiedt's Blog

  • | Post Points: 35
Not Ranked
Posts 2
Points 40
c0de replied on Mon, Apr 28 2008 5:58 AM

Ben,

Thank you for the advice. I'm going to try it for 2008 and will get back to you in case we have difficulties, if you wouldn't mind.

Thank you,

Anton

  • | Post Points: 20
Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers
Ben Tiedt replied on Mon, Apr 28 2008 1:31 PM

No problem.  We're here to help.

Ben Tiedt's Blog

  • | Post Points: 5
Not Ranked
Posts 8
Points 160

In CS2008 class User d'nt contain property "Theme". How I understand if change theme via CSContext.SiteTheme, than theme of site will change for all users?

Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers

Alex Kler:
How I understand if change theme via CSContext.SiteTheme, than theme of site will change for all users?

The CSContext object only exists for a single user/request -- setting CSContext.SiteTheme as described above will only affect the current user.

Ben Tiedt's Blog

  • | Post Points: 20
Not Ranked
Posts 8
Points 160

Ben Tiedt:

Skip...

The CSContext object only exists for a single user/request -- setting CSContext.SiteTheme as described above will only affect the current user.

Thak you for answer. Smile

If the user, is not logged (anonymous) and the theme was is changed for him through CSContext. SiteTheme, for another anonymous the theme will be changed too?

Not Ranked
Posts 8
Points 160

The default theme is "Theme 1"

I was try use:

CSContext.Current.SiteTheme = "Theme 2"

But theme was changed incorrectly.

The following picture is observed: on site loaded master page from "Theme 1", but layout was appled from "Theme 2"

  • | Post Points: 20
Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers

Alex Kler:
If the user, is not logged (anonymous) and the theme was is changed for him through CSContext. SiteTheme, for another anonymous the theme will be changed too?

No, each request gets a new CSContext which lives only for the duration of the request.  So each setting of CSContext.SiteTheme is specific to a single request.

Ben Tiedt's Blog

  • | Post Points: 5
Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers

Alex Kler:

The default theme is "Theme 1"

I was try use:

CSContext.Current.SiteTheme = "Theme 2"

But theme was changed incorrectly.

The following picture is observed: on site loaded master page from "Theme 1", but layout was appled from "Theme 2"

Ensure that you are setting CSContext.SiteTheme via an HTTP Module during the AuthenticateRequest event.  This is the appropriate time to change the site theme for the request and have the change carried into the full rendering of the page.

It appears that you've changed CSContext.SiteTheme after some theme-related logic has already processed.

Ben Tiedt's Blog

  • | Post Points: 20
Not Ranked
Posts 8
Points 160

Ben Tiedt:

Skip...

Ensure that you are setting CSContext.SiteTheme via an HTTP Module during the AuthenticateRequest event.  This is the appropriate time to change the site theme for the request and have the change carried into the full rendering of the page.

It appears that you've changed CSContext.SiteTheme after some theme-related logic has already processed.

Wow!Big Smile Thank you very very match! Its worked. 

But how to change themes for Blogs under the same scheme? How I can get access to Blogs from CSContext?

If I use Weblogs.GetWeblogs, themes vary permanently, but it is necessary to us only for current session.

Thank you again.

  • | Post Points: 20
Top 10 Contributor
Posts 3,424
Points 65,735
CS Developers

Alex Kler:
But how to change themes for Blogs under the same scheme? How I can get access to Blogs from CSContext?

CSContext.SectionTheme can be set to the name of the blog theme that you want to use for the current request.  This property should be set at the same time that CSContext.SiteTheme is set.

Ben Tiedt's Blog

  • | Post Points: 5
Page 1 of 1 (12 items) | RSS
Powered by Community Server (Commercial Edition), by Telligent Systems

Copyright© 2008 Telligent Systems Inc. All rights reserved
CommunityServer.com  •  Telligent.com