Create a WebCreated event of your own in onet.xml

I just delivered an internal MOSS IW Ramp course in Münich and we have some discussions concerning the onet.xml and how to modify the content of the sites, which have been created using custom code directly after site has been created. You are most likely aware that during site provisioning, there's no such event available as the WebCreated, which would be raised when the web creation based on onet.xml has been done. This is quite a huge limitation, but then again, there are quite good workarounds for this.

Possible workarounds

  1. Create your own custom site definitions using browser and export that using WSS 3.0 Extensions for VS 2005. As part of the export process, the tools generate the necessary code and associations to be used. Unfortunately the extensions do not support WCM enabled sites, so it cannot be used in publishing sites.
  2. Use ExecuteUrl element to redirect the user to custom aspx page after the site has been created to be able to customize the site. Your custom aspx page might be located under the _layouts folder and there for we can easily point to that one using the xml elements within the onet.xml.
  3. Create the WebCreated event using the powerfull feature framework. I'll declare the steps below for more detailed information.

 

Steps for WebCreated event using feature receiver

Step-by-Step guide for manual creating of WebCreated event are the following:

  1. Create feature to deploy the default.aspx to root of the site using Module events
  2. Create feature with feature receiver class – This is used for the Site Created event
  3. Create site definition, which does not have directly default.aspx, rather use the feature developed above
  4. Set the order of the features so that the default.aspx feature is activated before the receiver feature
  5. In feature receiver feature, access the default aspx page, for example access the web part manager of the welcome page using following code

//look for the default page so we can mess with the web partsSPFile thePage = curWeb.RootFolder.Files["default.aspx"];

//get the web part manager SPLimitedWebPartManager theMan = thePage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

 

Final words

This was extremely quick sample, but hopefully it's useful to you. I'll try to find some time to make more comprehensive example of this.