The Microsoft.SharePoint.Utilities namespace contains a class called ThmxTheme which gives us some nice utility methods to interact with site themes in SharePoint 2010. Here is an example of how to automatically set the theme for a newly created SPWeb to be the same as the root web-

Do you know of a nicer way to inherit site themes? Please post a comment below.

UPDATE (09/09/10): An alternative way to do this-

5 Responses to Inherit Site Theme programmatically

  1. Justin says:

    I cannot seem to get this code to function.

    newWeb does not exist

  2. Pranav says:

    @Justin – newWeb is just a variable name that refers to a SPWeb object that represents the web where you want to apply the new theme.

  3. Justin says:

    I understand that.

    Currently I have newWeb = SPContext.Current.Web

    This does not function on a new site being created, as it does not offically exist yet.

    What do you have your variable set to?

  4. Pranav says:

    @Justin – I ended up using a feature that is stapled to the GLOBAL template. Presumably you could also attach an event receiver to the new WebProvisioned event. Here is some sample code-

    class ApplyTheme : SPFeatureReceiver
    {
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
    base.FeatureActivated(properties);
    using (var web = properties.Feature.Parent as SPWeb)
    {
    if (web != null)
    {
    ThmxTheme theme = ThmxTheme.Open(web.Site, “_catalogs/theme/Fraunhofer.thmx”);
    theme.ApplyTo(web, true);
    }
    }
    }
    }

  5. Justin says:

    Thanks, I will give that a go.

    I have attempted to use an event receiver on the web provisioned and the web was provisioned events receivers, with no success.

Leave a Reply

Your email address will not be published.

Set your Twitter account name in your settings to use the TwitterBar Section.