Custom Site Definition for Creating Sub Site

Suppose you create a Custom Site Definition.  Now when the user creates an instance of this you also want to create a SubSite (from a different Site Definition) in the current site. Below is the solution to achieve this.

The way to do this is through the publishing assemblies in the webtemp file of your Site Definitions. SharePoint uses the webtemp files to point to the correct site definition when creating the sites. Usually the webtemp files contain an xml section that points to one site definition. However, the publishing sites point to an assembly by filling in the provision assembly attribute of the xml. It also takes in an xml file as the parameter for the assembly. This xml file has the structure of the subsites you want to create. I recommend looking at the webtemp files for the publishing sites to see how this is done.

Here is a quick example of an xml node out of a webtemp file I use for demos:

<Template Name=”DemoCompanySiteDefinitionPortal” ID=”10001″>

<Configuration ID=”0″ Title=”Custom Site Definition with subsites” Hidden=”FALSE”

ImageUrl=”/_layouts/images/blankprev.png”

Description=”Builds a custom site definition with subsites”

ProvisionAssembly=”Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”

ProvisionClass=”Microsoft.SharePoint.Publishing.PortalProvisioningProvider”

ProvisionData=”xml\\MyCustomSubsiteStructure.xml”

RootWebOnly=”TRUE”

DisplayCategory=”DemoCompany” Type=”0″ />

</Template>

Notice the ProvisionAssembly, ProvisionClass and ProvisionData. The ProvisionAssembly and ProvisionClass are the out of the box assemblies SharePoint provides for you. So, don’t change them. The ProvisionData class is the parameter that the ProvisionClass takes in. This is the xml file that defines your subsite structure.

<?xml version=”1.0″ encoding=”utf-8″ ?>

<portal xmlns=”PortalTemplate.xsd”>

<web name=”Home”

siteDefinition=”DemoCompanySiteDefinition#0″

displayName=”$Resources:cmscore,IPPT_Portal_Root_DisplayName;”

description=”$Resources:cmscore,IPPT_Portal_Root_Description;” >

<webs>

<web name=”SubSite1″

siteDefinition=”DemoCompanySiteDefinitionSubSite#0″

displayName=”Sub Site”

description=”Custom Demo Company sub site.” />

</webs>

</web>

</portal>

Notice that the xml structure of the ProvisionData contains the schema PortalTemplate.xsd. This schema is a simple one that just has “Webs” and “Web” nodes. You need one root web and then you can build your subsite structure as deep as you want. Each web needs to point to a Site Definition and you have to put the #{number} to define the configuration in the Site Definition that you want to use.