Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > zsThemes explained!
August 24
zsThemes explained!

This post is about how the solution as described in a previous post called Adding Themes the supported way! actually works.

First of all, I wanted to get themes per WebApplication. To achieve this, I created three extension methods for the SPWebApplication class that do the following:

  • GetDefaultThemes, this method retrieves all the Themes that are defined in the SPThemes.xml
      public static DataView GetDefault()
      
      {
      
          string filePath = String.Format("{0}\\LAYOUTS\\1033\\SPThemes.xml", SPUtility.GetGenericSetupPath("TEMPLATE"));
      
          SPThemes spThemes = new SPThemes();
      
          spThemes.DataSetName = "SPThemes";
      
          spThemes.Locale = new CultureInfo("en-US");
      
          spThemes.Namespace = "http://tempuri.org/SPThemes.xsd";
      
          spThemes.ReadXml(filePath);
      
          DataView view = new DataView(spThemes.Tables[0]);
      
          view.Sort = "DisplayName";
      
          return view;
      
      }
  • GetCustomThemes, this method retrieves all the custom themes
      public static DataView GetCustom(SPWebApplication webApplication)
      
      {
      
          List<string> filePaths = new List<string>();
      
          filePaths = GetOtherXmlDefinitions(webApplication);
      
          DataTable dataTable = new DataTable();
      
          dataTable = ProcessOtherXmlDefinitions(filePaths);
      
          return new DataView(dataTable);
      
      }
      
      private static List<string> GetOtherXmlDefinitions(SPWebApplication webApplication)
      
      {
      
          List<string> filePaths = new List<string>();
      
                      
      
          ThemesXmlFileLocations themeFileLocations = webApplication.GetChild<ThemesXmlFileLocations>("ThemesXmlFileLocations");
      
          if (themeFileLocations != null)
      
          {                
      
              filePaths = themeFileLocations.FilePaths;
      
          }
      
          return filePaths;
      
      }
      
      private static DataTable ProcessOtherXmlDefinitions(List<string> filePaths)
      
      {
      
          DataTable dataTable = new DataTable();
      
          
      
          foreach (string filePath in filePaths)
      
          {
      
              if (!string.IsNullOrEmpty(filePath))
      
              {
      
                  string customFilePath = SPUtility.GetGenericSetupPath(filePath);
      
                  
      
                  SPThemes spThemes = new SPThemes();
      
                  spThemes.DataSetName = "SPThemes";
      
                  spThemes.Locale = new CultureInfo("en-US");
      
                  spThemes.Namespace = "http://tempuri.org/SPThemes.xsd";
      
                  spThemes.ReadXml(customFilePath);
      
                  dataTable = spThemes.Tables[0].Clone();
      
                  foreach (DataRow row in spThemes.Tables[0].Rows)
      
                  {
      
                      DataRow customThemeRow = dataTable.NewRow();
      
                      customThemeRow.ItemArray = row.ItemArray;
      
                      dataTable.Rows.Add(customThemeRow);
      
                  }
      
              }
      
          }
      
          
      
          return dataTable;
      
      }
  • GetAllThemes, as the name suggests, this method gets all the Themes and merges the above.

 

Secondly I needed to add custom themes without touching the SPThemes.xml file.  As you may have noticed I make use of a custom persistent object called “ThemeXmlFileLocations”. This object only has one field which is a generic list of strings. This list holds all the filelocations which are added. So with each filelocation, I look in the custom .xml file and add the custom themes to the datatable as you can see in the “ProcessOtherXmlDefinitions” method above. This is, basically, the trick to add themes without touching the core SPThemes.xml file. So here below is the persistence class:

public class ThemesXmlFileLocations : SPPersistedObject
{
    [Persisted]
    public List<string> FilePaths;
    public ThemesXmlFileLocations() { }
    public ThemesXmlFileLocations(string name, SPPersistedObject parent, Guid id) : base(name, parent, id) { }
}

I don’t it’s really necessary to put the code up here to show you how you can add a filelocation string to the list of the class eh? :)

 

Thirdly I needed to create a custom ThemeSelection page so all the other themes are also presented to the user. To do that, I copied the out-of-the-box page and in the codebehind put some more logic to

a) Get all the themes that are installed in the current WebApplication by making use of my extension methods

DataTable allThemesTable = 

SPContext.Current.Site.WebApplication.GetAllThemes().ToTable();


b) Filter the themes if they are blocked by an administrator

 

I also want to note and be clear about is that adding themes this way and choosing them only works by making use of a custom application page that replaces the out-of-the-box themeselection page. Meaning that users can still navigate to the out-of-the-box one and miss out on the custom and/or blocked themes.

So I hope the code makes more sense..  as said before, the code is available CodePlex for you to check out! ;)

Comments

There are no comments for this post.
 

 Statistics

 
Views: 938
Comments: 0
Tags:
Published:998 Days Ago