One of new functionalities introduced in SharePoint 2013 Preview is new theming engine. Themes that are managed by this new engine don’t use Office Theme .thmx format and can’t be created using PowerPoint like it was case before. New themes are based on set of xml files stored in Theme Gallery “15” subfolder (on relative path _catalogs/theme/15):
- .spcolor files which define color palettes for components of SharePoint interface
- .spfont files which contain set of predefined font schemes.
There are 30 color palette and 3 font scheme files stored in this subfolder, but it is also possible to upload new .spcolor and .spfont files, as long as your files correspond to predefined XML format.
I still haven’t found automatized way of theme creation, but one possible approach would be to download existing files from library, edit color values in text editor, save files with new name but same extension and upload new files back to the library. You can learn more about internal structure of SPTheme and SPColor files in this blog post by Ello Struyf: Themes and Composed Looks in SharePoint 2013.
Let’s write some code
New theming engine has corresponding API in SharePoint 2013 Server Object Model. This API enables developers to create and apply new themes based on existing Color and Font files. This functionality is in SPTheme class stored within Microsoft.SharePoint.Utilities namespace. Following code snippet ilustrates one possible approach to applying specific color palette to specific SPWeb:
SPFile spColorFile = targetWeb.GetFile(targetWeb.ServerRelativeUrl + "/_catalogs/theme/15/Palette002.SPCOLOR"); SPTheme spTheme = SPTheme.Open("ProgrammedTheme", spColorFile); spTheme.ApplyTo(targetWeb, true);
I will now briefly describe functionality of this code snippet shown above:
- In first line, existing color palette from theme folder is opened, and loaded into SPFile object.
- Afterwards, SPTheme is created with name “ProgrammedTheme” and using SPFile that contains color palette.
- In the end, SPTheme is applied to target web. Second parameter of ApplyTo method determines whether the applied theme will be shared with other sites in same site collection or used only to targetWeb. This parameter also determines what will happen to the theme after it is not used anymore.
How do I implement this?
For the sake of testing, code shown above can be encapsulated in console application. But, in any more serious case it should be used in SharePoint context (for example, in SharePoint Feature “Feature Activated” event).
Until next time… Dragan