SharePoint 2010: Automatically Reset to the Default Theme in Code

When you want to reset your current theme that has been applied to your SharePoint site in code, you may try code like:

web.ApplyTheme("default");

but that won’t work because there is no “default” theme.
After looking in the SPTHEMES.XML file found in the C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATELAYOUTS1033 folder, you do see there is a theme with a theme name of “none”. That makes sense so you try:

web.ApplyTheme("none");

But often that still won’t work. Frustrating. Keep searching and after a review of Microsoft’s documentation on the SPWeb.ApplyTheme method, you see it.

Caution Use this method only when the UI is in backward compatibility mode (SPWeb.UIVersion = 3).

Not too many of us use UIVersion = 3 in SharePoint 2010, so what do you do now?

Easy. Use the replacement class, ThmxTheme (found in Microsoft.SharePoint.Utilities), in particular the ThmxTheme.RemoveThemeFromWeb method.
Example:

ThmxTheme.RemoveThemeFromWeb(site, true);

Where site is the SPWeb and the bool value states if you wish to also remove the theme’s styles. Setting this to true will remove the theme specific styles that was created when the theme was applied to the web, but I have found that setting this to true can cause issues because of security concerns. I suggest you try settings the bool value to true if you wish to have this method remove theme styles files, but if you receive errors, set this to false and it will likely work. This is not the best for housekeeping, but may make your life easier.

As a quick example, say in a feature receiver when you deactivate a feature, the following code would remove the theme from an entire site collection if the feature is scoped to the site level.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { 
 //go and get the root site collection SPSite 
 siteCollection = properties.Feature.Parent as SPSite; 
 if (siteCollection != null) { 
  SPWeb topLevelSite = siteCollection.RootWeb; 
  foreach (SPWeb site in siteCollection.AllWebs) { 
   //reset the theme in case one was applied 
   ThmxTheme.RemoveThemeFromWeb(site, true); 
  } 
 } 
}

One thought on “SharePoint 2010: Automatically Reset to the Default Theme in Code

  1. Mikel October 25, 2012

    Thanks for this article! I was about to have a fit over ApplyTheme not working. Your post helped saved my sanity 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Enter Code *

Filed Under

Related

Are you preparing to launch a new #SharePoint #intranet?

Don’t spring it on employees out of the blue. First, talk to as many of them as you can.

Planning is just phase one. For all of our tips, watch our recent webinar with @EricOverfield!

https://pixelmill.com/webinar-recap-9-takeaways-from-a-migration-to-sharepoint-online/

Are you preparing to launch a new #SharePoint intranet?

Don’t spring it on employees out of the blue. First, talk to as many of them as you can.

Planning is just phase one. For all of our tips, watch our recent webinar!

http://ow.ly/dsQM50GqPI5

It's that time again! Join #Microsoft MVP & Regional Director, @EricOverfield on Thursday, October 28th, at 11:00 AM PST as he walks through #MicrosoftViva’s current state & offerings and what they mean to your #digitalworkspace right now.

http://ow.ly/3QFK50Gnx9f

It's the final countdown! Join @EricOverfield at 11am PST to learn about common pitfalls and paths to success when moving to #SharePointOnline. You'll walk away with practical tips to use in your organization today!

http://ow.ly/fdf150GjyJT

In today's episode of the #PixelMillWebinars, you'll learn from the experience of a global organization’s path to digital teamwork victory and get to see first-hand how to successfully plan a smooth migration for your team.

Join us at 11am PST!

https://pixelmill.com/webinar-9-takeaways-from-a-global-organizations-migration-to-sharepoint-online/

Subscribe to PixelMill's
E-news

* indicates required

Let's Talk Digital
Workspaces Today

Get In Touch