Friday, April 07, 2006

Automatically applying a theme to a WSS site when it is created

On his blog, Ishai Sagi describes a method he has found to automatically apply a certain theme after it has been created.

"WSS site definitions dont support setting a default theme. This leave the administrator with three options, none of them recommended:

  1. Have users manually set a theme after creating a site (ugly!)
  2. Set the company theme to a site, and save it as template and deploy the template globaly in the server (complicated, and also disconnects the sites from the file system templates, making it hard to change in the future)
  3. Change the default css files and not use the theme (extremely ugly - why are the themes for??? also does not support multiple templates with different themes)

The solution I found for the problem is to add in the site definition a link to a custom page that will run code when the site is created. the code will apply the theme to the new site.

Step 1 - Changing The Site Definition

  1. Create the site definition that you want
  2. Go into the "xml" folder and open the "onet.xml" file in notepad or visual studio (or any editor)
  3. Find the "Configurations" tag at the bottom, and for every configuration you want to change add the following in the "Configuration" tag (where it says "THEMENAMEHERE" write your theme name. This may be case sensative):
    <ExecuteUrl Url="_layouts/1033/ThemeSetter.aspx?Theme=THEMENAMEHERE" />

Step 2 - Creating the ThemeSetter ASPX Page

  1. Open "C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\LAYOUTS\1033"
  2. Create a new text file, rename it to "ThemeSetter.aspx"
  3. Open the file for editing, and paste the following code into it:

<html dir="ltr">
<%@ Page Language="C#" ValidateRequest="False" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>

SPWeb myNewWeb = SPControl.GetContextWeb(Context);
myNewWeb.AllowUnsafeUpdates = true;


Step 3 - Reset the IIS
for the changes to take affect, you will need to reset the IIS.

Now, create a site from the site definition, and the site should automatically be with the theme."


No comments: