Automatically running a program after creating a site
For those who have jumped into the mysteries of creating site templates without SharePoint, here is a little tip to execute code after the user created a site based on the template. I used this trick to automatically grant anonymous access to a site that used a public facing template.
Inside the <Configuration> element in ONET.XML (located in the XML folder for your template definition), add a new element called <ExecuteUrl>. Within this element add an attribue called “Url” that defines the URL that is called following instantiation of the site definition. Granted this will usually point to an ASPX page but if the page is blank and you call code in the Page_Load event then you can perform the processing you need. Just remember to redirect back to the home page of your site when you are finished. I used:
SPWeb thisWeb = SPControl.GetContextWeb(Context);
Response.Redirect (thisWeb.Url);
Here is what my <Configuration> element looks like (more or less). The ExecuteUrl element is in red.
<Configuration ID="1" Name="Public">
<Lists>
<List Title="Shared Documents" Url="Shared Documents" QuickLaunchUrl="Shared Documents/Forms/AllItems.aspx" Type="101"/>
...
<List Title="List Template Gallery" Type="114" Url="_catalogs/lt" RootWebOnly="TRUE"/>
</Lists>
<Modules>
<Module Name="Public"/>
<Module Name="WebPartPopulation"/>
</Modules>
<ExecuteUrl Url="_layouts/1033/EnableAnon.aspx"/>
</Configuration>