views:

152

answers:

1

Hi All,

I have a requirment where in my sharepoint site I want to set the theme according to user.

for e.g lets say if user a set his theme as theme1 and the user b logs in and set theme to theme2. So next time when user a log in he must have to see the theme set by him. I.e theme a.

Can any one tell me what will be the best approch to do it.

Thanks in advance.

Sachin

A: 

I had a similar requirement once. In my case they wanted users to be able to change the "color layout" of a MOSS portal (so the layout and fonts was the same, but background color and colors of images were different in each theme). I created a "base theme" which included a complete layout (one of the provided themes) as a single CSS file. Then I created additional themes, such as "blue.css", "red.css", "green.css" et cetera and put all those files in portal/ourthemes/.

We wanted the users to be able to choose their theme, so we created a new user profile property "CurrentTheme" (Sharepoint Central Administration -> Shared services -> User profiles and properties -> Add profile property) which was defined as string with a pre-defined list of choices.

Then I created a simple ASP.Net control which rendered as

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
  Dim oProf As Microsoft.Office.Server.UserProfiles.UserProfile = Microsoft.Office.Server.UserProfiles.ProfileLoader.GetProfileLoader.GetUserProfile()
  Dim UserTheme As String
  Try
    If oProf.Item("CurrentTheme") IsNot Nothing Then
      UserTheme = oProf.Item("CurrentTheme").Value.ToString()
    Else
      UserTheme = "blue"
    End If
  Catch ex As Exception
    'shouldn't fail if we don't know the value
    UserTheme = "blue" 'a default value for users who dont have a theme yet
  End Try

  writer.WriteLine("<link rel='stylesheet' type='text/css' href='/portal/ourthemess" & Trim(UserTheme) & ".css' />")
End Sub

(Disclaimer: the actual code was a bit longer, because we used caching per-user to avoid reading the property from UserProfile every time user loaded the page)

Then I put this control in the master page created for that portal.

EDIT: To do the caching, we created a cache key which contained user name and stored the generated text in there. The result was something like this:

Dim KeyName As String = Page.User.Identity.Name & "_CurrentTheme"
If (Not Me.Page.Cache.Item(KeyName) Is Nothing) Then 
   writer.Write(Page.Cache.Item(KeyName).ToString)
Else 
  '...code posted previously goes in here

  'at the end
  Me.Page.Cache.Add(KeyName, _
              AllContentRenderedInPreviousCodeAsString, _
              Nothing, _
              Caching.Cache.NoAbsoluteExpiration, _
              Caching.Cache.NoSlidingExpiration, _
              Caching.CacheItemPriority.Low, Nothing)
End If
naivists
hi naivistsThanks for u r quick reply. It would be great if you could give me the full source code. so that i can have clear understanding of caching per user.
Sachin
added some code to show how it was implemented. Yes, another thing is that we actually created a webpart where user could choose the theme (choosing from a preview screen-shot). The webpart would then set the `UserProfile` property and clear out the cache value.
naivists
hi naivists...Thanks for your help .This will definetly going to help me alot..
Sachin