User specific theme in sharepoint

不问归期 提交于 2019-12-25 06:46:30

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/2153310/user-specific-theme-in-sharepoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!