Tuesday, March 24, 2009

Executing some code on site creation

Scenario:
While discussing a requirement with my collegue , we wanted to execute some code on site creation / provisioning.

Solution:
SPWebProvisioningProvider is your answer.

Code:

using Microsoft.SharePoint;
namespace Training
{
public class CustomProvisioningProvider: SPWebProvisioningProvider
{
public override void Provision(SPWebProvisioningProperties props)
{
props.Web.ApplyWebTemplate(“STS2#0”);
props.Web.Title = props.Data;
props.Web.Update();
}
}
}
WebTempTraining.xml:
<?xml version="1.0" encoding="utf-8"?>
<Templates xmlns:ows="Microsoft SharePoint">
<Template Name="STS2" ID="10020">
<Configuration ID="3" Title="Team Site New"
Hidden="FALSE"
ImageUrl="/_layouts/images/stsprev.png"
Description="This is Team Site New."
DisplayCategory="My Site Templates"
ProvisionClass="ClassLibrary.CustomProvisioningProvider"
ProvisionAssembly="ClassLibrary, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=2b56bd310cddb0e3"
ProvisionData="My Team Site New" />
</Template>
</Templates>
Note:
-- As per MS documentation this get fired as soon as web is created but it might be possible that lists are still not provisioned. So accessing any list might throw Exception

-- Be careful about calling the ApplyWebTemplate method within a Web site provisioning callback. Calling this method inside a provisioning callback that is defined within the same site definition configuration that is being applied can cause an infinite loop. Instead, create two similar site definition configurations within the site definition, one that is visible and one that is hidden. The configuration can then contain a provisioning assembly callback that applies the hidden configuration to Web sites.

Articles:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spwebprovisioningprovider.provision.aspx

0 comments: