Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom web template used to provision site collections that activates features with onet.xml and is deployed as a farm solution. I've tried both feature receivers and event receivers (on WebProvisioned from the solution proposed here: Is feature activation code allowed in Web Template Project?) and neither are firing when a new site collection (or web) is provisioned using the web template or when the feature is activated.

Is there something I'm missing? Is there anyways to fire off custom code after a site collection is provisioned using a web template? Thanks.

share|improve this question
add comment

4 Answers

The WebProvisioned method of the SPWebEventReceiver does not fire when a site collection is created. This is confirmed by the WebProvisioned Method MSDN page.

Depending of what your custom code does, it is best to use feature stapling either scoped to Site or Web against the site template you are using.

share|improve this answer
1  
I don't think feature stapling works with webtemplates either. –  Steve Lineberry Nov 4 '11 at 0:06
    
I couldn't get feature stapling to work either –  Adam Nov 7 '11 at 17:13
add comment

I experienced a similar issue. Upon creation of a new site, the features included in the <WebFeatures> node of the ONet.xml were not activated upon site creation. When navigating to '/_layouts/ManageFeatures.aspx' these features would appear to be activated but their code would not run (no files were placed from modules, breakpoints set in event receivers would not be reached). I was able to resolve the issue by making a change in my Feature nodes. Originally the nodes I used were copied from an ONET extracted from a .wsp that had been generated using the 'Save Site as Template' feature. Those nodes looked like this:

<Feature ID="{5bd39d1e-9e39-4e4f-932c-eb393b5454a4}" Name="FeatureDefinition/5bd39d1e-9e39-4e4f-932c-eb393b5454a4" SourceVersion=0.0.0.0" /> 

When I modified the Node to resemble this:

<Feature ID="{5bd39d1e-9e39-4e4f-932c-eb393b5454a4}" /> 

The features activated as expected.

share|improve this answer
add comment

It kind of sucks but what I did in my cases is to extract the files from the webtemplate.wsp. Then edit the onet.xml file as I see fit with the features I need. Then I repackage the files/folders back into a cab/wsp.

share|improve this answer
    
I'm sorry, but I'm not sure how that relates to adding custom code? Can you please elaborate? Thanks! –  Adam Nov 7 '11 at 17:17
    
You said you are using a web template and not a site definition, correct? Your custom code would be a feature that is activated when a site is created from the web template. Since web templates don't support feature stapling like site definitions do, what I did is configure a site how I want it and save it as a web template. Then I could extract the files and edit the onet.xml to activate the features that I want activated when this template is chosen. I then repackaged them as a wsp and added it to SP. –  Steve Lineberry Nov 8 '11 at 18:42
add comment

You can put your feature receiver code in a second feature. It can still be deployed as part of the same package, but would be activated separately.

The basic scenario is a feature reference in the onet.xml and then the 2nd feature is scoped for the web it is being activated on. When a new site collection is provisioned based on the web template, it will attempt to activate the web feature and perform the rest of the configuration. The great thing about this, is that since all of that configuration is maintained in a separate feature you can take advantage of the feature upgrading concepts in 2010 to maintain those sites previously provisioned.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.