Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

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…

Posted on Saturday, August 29, 2009 6:32 PM | Back to top


Comments on this post: Feature Stapling a Theme to a Site Definition – The Right Way

# re: Feature Stapling a Theme to a Site Definition – The Right Way
Requesting Gravatar...
I don't think you should be disposing of this SPWeb.
Left by Brian Bedard on Aug 30, 2009 4:38 PM

# re: Feature Stapling a Theme to a Site Definition – The Right Way
Requesting 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.
Left by MOSSLover on Aug 30, 2009 6:40 PM

# re: Feature Stapling a Theme to a Site Definition – The Right Way
Requesting 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(
Left by Paul Schaeflein on Aug 31, 2009 11:17 AM

# re: Feature Stapling a Theme to a Site Definition – The Right Way
Requesting Gravatar...
Does spdisposecheck offer any advice one way or the other?
Left by Paul Galvin on Sep 01, 2009 7:10 AM

# re: Feature Stapling a Theme to a Site Definition – The Right Way
Requesting Gravatar...
Good info. I'm also curious if spdisposecheck caught this.
Left by doug b on Nov 08, 2009 7:30 PM

Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net