HOW TO: Create a SharePoint Web Template Provision Class 

WSS has a neat little feature that allows you to override the default site provisioning behavior as defined in the onet.xml file. When the WSS runtime detects that you have a provision class associated with a template, it ignores the provisioning definition in the onet.xml file and calls your custom provision class. This opens many possibilities for provisioning site in any way that you see fit. If you wanted to replace the default onet.xml provisioning, that is possible. Or if you want to apply another web template then provision files, list items, etc., no problem as well. Here's how it's done:

  1. Create your provision class. This class will be called by the WSS framework when your template is instantiated. It must inherit from the Microsoft.SharePoint.SPWebProvisioningProvider class and implement the Provision(SPWebProvisioningProperties) method. As you might expect this method is called when the web is provisioned by the WSS framework.
    public class WebProvisionProvider : SPWebProvisioningProvider { 
        public override void Provision(SPWebProvisioningProperties props) { 
            SPWeb rweb = props.Web;
            string data = props.Data;
            // Write code here to provision lists, documents, sub webs, etc 
            // that are necessary for this web template.
        } 
    }
    In this example I am applying a web template as specified by the SPWebProvioningProperties.Data property. This property provides a mechanism to pass arbitrary data into your provisioning class. Here, I am using the data property to pass in the actual template I'd like to apply to the new web. After the template is applied I am calling my custom provisioning framework, which imports content (such as files, pages, list items) and also creates predefined child webs.

  2. Wire your provision class to your template. In this step we tell SharePoint to use our provisioning class when a specific site template is instantiated. For this you will need to create a webtemp.xml file (see the SDK if you haven't done this before). Add something similar to the below xml to your webtemp file. This tells SharePoint that you want to execute your custom provision class when this template is instantiated.
    <Configuration ID="100" 
        Title="My Site Template" 
        Description="My Site Template" 
        ImageUrl="/_layouts/images/stsprev.png"
        Hidden="FALSE" 
        DisplayCategory="Publishing"
        ProvisionAssembly="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11df43608d33992f"    
        ProvisionClass="MyAssembly.WebProvisionProvider"
        ProvisionData="" />

Comments
cool stuff. Thanx.
That’s great, I never thought about Create a SharePoint Web Template Provision Class like that before.
yeah ! thank a lot for this post, you saved my day :o)
Add a New Comment
Name

Email Address

Url

Comment