Thursday, March 09, 2006

Automatically applying a theme to a site when it is 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 (thanks to Kalmberg) 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.

Kalmberg example was instructive, but allow me to take you through the proccess step by step:

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=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%
    
    SPWeb myNewWeb = SPControl.GetContextWeb(Context);
    myNewWeb.AllowUnsafeUpdates = true;
    myNewWeb.ApplyTheme(this.Page.Request["Theme"].ToString());
    myNewWeb.Update();
    Response.Redirect(myNewWeb.Url);
    
    %>
    </html>

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.

Liked it? kick it on SharePointKicks.com

10 comments:

Shane Perran said...

Good job on your posts, I just found your blog.

Glad to see others posting some great new tips and tricks :)

Cheers,

Shane
www.grapicalwonder.com

Anonymous said...

Very useful, thank you! Although I wish I would have found this hint two years ago. We choose option #2 posted on md2d, apply theme and save it as .stp. thx again.

Anonymous said...

Hi,

I used this solution in SPS 2003 implementations. Any idea how this is dealed with in MOSS 2007?

Greetz,
Stef.

Anonymous said...

Executeurl works, when i try to create site within site collection.

But execute url is not working with if create site from central admin but the site is created theme is not applied.

Frank-Ove Kristiansen said...

Hi.
I've put this into my site definition (onet.xml) in MOSS, and it is working as it should when creating the site within a site collection.

But, when I try to create a new site with this site definition from Central Administration, it doesn't do anything. Funny thing is, if I enter the url I want it to execute directly in the browser after the site is created, it does the work as it should (i.e. changing the Theme).

Bug?
Have you found out anything?
Thanks.

Richard said...

Hi

I tried this on a My Site definition with in MOSS. There was no effect after following all the steps. is this a knows issue with My Sites?

Webpriestess said...

Yo! I created a custom site definition in Moss 2007. I added this little trick and it worked beautifully. All's I did was change the assembly version from 11.0.0.0 to 12.0.0.0.

I am getting a funky thing going on with my header, but that's probably just me :)

Anonymous said...

For MOSS, you can use an eventhandler. Create a feature and activate it within a site definition. Specify what should be done to the site in the FeatureActivated eventhandler.

When I found the Execute Url method I also found in some article (on MSDN I think) that it will not work when the site is created through the admin UI og through an Office app - must be created in a sitecollection.

This properly also goes for MOSS (?), so the safest way is to use the FeatureActivated eventhandler. This will apply a theme (or any other customization, etc) to the site no matter how it is created.

Cheers,
Kalmberg

Anonymous said...

It also won't work with meeting workspaces.
Man why did they make this so difficult? All we want is to brand our sites.

Gergana Krumova said...

Thanks for the idea. I implemented the same thing using a feature and a feature receiver, look like that:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = null;
if (site == null)
{
web = properties.Feature.Parent as SPWeb;
if (web == null) return;

site = web.Site;
}
else web = site.RootWeb;

SPWeb rootWeb = site.RootWeb;

web.AllowUnsafeUpdates = true;
web.ApplyTheme("Verdant");
web.Update();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

}