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"); } } } } }}
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,
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.
No problem. We're here to help.
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?
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: Skip... The CSContext object only exists for a single user/request -- setting CSContext.SiteTheme as described above will only affect the current user.
Skip...
Thak you for answer.
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?
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"
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.
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: 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! 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.
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.
Copyright© 2008 Telligent Systems Inc. All rights reserved CommunityServer.com • Telligent.com