We all know about the concept of Feature Stapling and most people write it out correctly on there blogs, such as this post: http://www.sharepointnutsandbolts.com/2007/05/feature-stapling.html. However, I’ve noticed that very few blogs have the correct code to post in the FeatureActivated and FeatureDeactivating events. This is the correct code from looking at a CodePlex Project by Scott Hillier, looking at wrong code all over blogs, and debugging some code I cobbled together in Visual Studio to throw in FeatureActivated and FeatureDeactivating :
SPWeb Web = properties.Feature.Parent as SPWeb
Web.ApplyTheme(“simple”);
Web.Update();
Remember you want to change the theme to “none” from “simple” in the FeatureDeactivating. I am amazed and surprised how many people out there have bad blog posts on 4 lines of code. Using SPSecurity.RunwithElevatedPrivileges is 100% wrong. You don’t need to call on SPSite at all. I know, because I just tested all those different methods and they do not work. I probably fall short sometimes, but it’s always a good idea to test what you are doing before you post it.
**Update**
I updated the code after talking to some people about whether you dispose properties.Feature.Parent or not. It’s a debate it looks like, but I like the reasoning from Dan Attis and Robert Bogue, if you don’t own it, then don’t dispose the object. Now my question is why are you not receiving some type of error? Is it because the function is done? To me it just seems a little weird that you can dispose of the object in the first place. You can’t dispose of a Context object, because it gives you an error message. Overall it looks like there is a hole in a lot of the disposal documentation. When I looked at the MS Documentation it does not talk about this specific object and disposal. Why not I have no idea…