Showing posts with label subsite. Show all posts
Showing posts with label subsite. Show all posts

March 19, 2007

MOSS: Creating subsites in site definition

Well, not really, but by using a MOSS 2007 Features you can automatically create sites below the site user just created. Other way to create sites is to use Provisioning.

FeatureEventHandler:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using(SPWeb web = properties.Feature.Parent as SPWeb)
    {
        // Check that subsites do not already exist
        if(web.Webs.Count == 0)
        {
            // Create subsites
            try
            {
                web.Webs.Add("", "SubSite1 Title", "", 1033, "", false, false);
                web.Webs.Add("", "SubSite2 Title", "", 1033, "", false, false);
                web.Webs.Add("", "SubSite3 Title", "", 1033, "", false, false);
            }
            catch(Exception e)
            {
                throw new SPException("Error when trying to create subsites. Error: " +
e.Message);
            }
        }
    }
}

Create new subsite Feature:

<feature id="7F4AA5EF-6A62-4a08-8565-DE39C02839E7"

title="Subsites"

description="Creates subsites for specific sites."

version="1.0.0.0"

hidden="TRUE"

scope="Web"

xmlns="http://schemas.microsoft.com/sharepoint/" receiverassembly="FeatureEventHandler,Version=1.0.0.0,Culture=neutral,PublicKeyToken=60a5c2355dc3549c"

receiverclass="Features.FeatureEventHandler">

</feature>