SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a webtemplate feature based on TeamSite project, but I want to add code to run when I create a site based on this template.

Any idea how to do this?

Thanks in advance for your help.

share|improve this question

Since we are talking SharePoint 2010 I would recommend using the WebProvisioned asynchronous event. This event is triggered when a subweb under your rootweb is created (note that it does not trigger when creating root site, if you need this, consider either feature stapling as Jeroen suggests, or a custom provisioning provider (CPP) as Charles suggests).

Each approach has its advantages, one of the reasons why I often for site provisioning prefer provisioning providers over feature stapling is race conditions. In short you cannot be sure when your feature is called if you have other stapled features in the same scope.

In SP2010 i tend to use CPP for site provisioning where i need code to execute for root site, and WebProvisioned event for everything else.

share|improve this answer

Your question is rather vague but I think what you are looking for is an implementation of SPWebProvisioningProvider. This is a class that can execute custom code when a new site is created.

More information can be seen here:

share|improve this answer
Dear Sir, I am working on Visual Studio 2010 and SharePoint 2010. I want to create a Site Template based on TeamSite template. So I created a webtemplate feature, but I want to run code in site provisioning – user2180 Feb 9 '11 at 13:24

You may want to consider Feature Stapling.

share|improve this answer

Your Answer


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