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>

4 comments:

  1. Hi,

    I need to do exactly this - except, I need to call yet ANOTHER site definition. So, for example, I have a Project site which will have subsites (A, B, C). A, B, & C will have a custom definition as well (they have custom form libraries with events & workflow attached). Can I trigger creation a the project level and create all 4 sites with the appropriate heirarchy? I'm kinda a Sharepoint noob, so as much detail is very appreciated!

    Thanks!

    ReplyDelete
  2. I believe there shouldn't be no problem in generating more than one level of sites. You could do it in code following my example or use site provisioning to create the subsites.

    Site provisioning might be the best solution for you, just make sure that the project site's site definition uses site provisioning to create subsites and the site definition of these subsites use site provisioning to create subsites under them.

    Good example of provisioning subsites can be found here

    Let me know if this solved/didn't solve your problem.

    ReplyDelete
  3. hi,

    I have a Site definition for site collection (i.e. a Top Level Site) which has 8 lists (created as list definitions and activated as site features within the Site definition onet.xml). NOTE: one of the list named as CUSTOMER has a event handler attached to the Itemaddint event.

    What i need to do is create a sub site definition for a subsite under the top level site. This sub site definition would be used for provisioning a subsite under the top level site (see above) programatically when a record to the CUSTOMER list is added.

    I have the top level site definition created and its working fine. but when i use a sub site defintion and try to create a subsite under the top level site. it gives me an error "Invalid template or template not found".

    Could you let me know how to go about creating a site definition for top level site and a sub site and use it programatically.

    Ram

    ReplyDelete
  4. Creating a subsite in ItemAdding might give you some timeout problems. Better way might be to try doing it in ItemAdded or fire up a separate thread where the subsite is created separately from the ListItem creation.

    Can you create the subsite under the root page normally using a browser? This should be your first task.

    After you have confirmed that the site definition itself is working you might want to create a simple Windows Forms application where you create the site programmatically.

    Finally after that is working you should try to move the site creation code into the event handler of the list.

    Let me know how your proceed...

    ReplyDelete