Feature Stapling a Theme to a Site Definition – The Right Way

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…

Print | posted on Saturday, August 29, 2009 6:32 PM

Feedback

# re: Feature Stapling a Theme to a Site Definition – The Right Way

Left by Brian Bedard at 8/30/2009 4:38 PM
Gravatar I don't think you should be disposing of this SPWeb.

# re: Feature Stapling a Theme to a Site Definition – The Right Way

Left by MOSSLover at 8/30/2009 6:40 PM
Gravatar I went in and did some research before and after. I could not find any definitive answer. However, two people stated above told me that you don't do it due to not owning it. I even went through MS Documents...Someone needs to add this to the main disposal documentation.

# re: Feature Stapling a Theme to a Site Definition – The Right Way

Left by Paul Schaeflein at 8/31/2009 11:17 AM
Gravatar You should save the previous setting to the SPWeb Properties property:
Web.Properties["OrigTheme"]=Web.Theme

Then, in FeatureDeactiving, apply that one:
string origTheme = Web.Properties["OrigTheme"];
if (String.IsNullOrEmpty(origTheme)
origTheme = "none";
Web.ApplyTheme(origTheme);
Web.ApplyTheme(

# re: Feature Stapling a Theme to a Site Definition – The Right Way

Left by Paul Galvin at 9/1/2009 7:10 AM
Gravatar Does spdisposecheck offer any advice one way or the other?

# re: Feature Stapling a Theme to a Site Definition – The Right Way

Left by doug b at 11/8/2009 7:30 PM
Gravatar Good info. I'm also curious if spdisposecheck caught this.

Your comment:





 
 

Copyright © MOSSLover

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski