1
votes

I'm working on a sharepoint 2010 publishing site that has many subsites. I've set up a custom master page, and several custom page layouts.

I've discovered how to set the default page layout used for newly created pages in a subsite (found at /_Layouts/AreaTemplateSettings.aspx), but I can't seem to figure out how to specify the default page layout used to create ~/Pages/default.aspx when I create a new subsite.

Right now it selects WelcomeLinks.aspx, and that's not what I want.

Is this only available if I deploy custom master pages / layouts via code, and if so, does anyone have any good examples?

Thanks.

4

4 Answers

4
votes

The Page Layout of a newly created subsite is determined by the site definition. For example, if you create a subsite using the Publishing Site with Workflow template, then that site is created using Configuration ID="2" from 14\TEMPLATE\SiteTemplates\BLANKINTERNET\XML\onet.xml. Within that configuration is a module section that points to SubWebWelcome:

<Module Name="SubWebWelcome" Url="$Resources:osrvcore,List_Pages_UrlName;" Path="">
    <File Url="default.aspx" Type="GhostableInLibrary" Level="Draft" >
        <Property Name="Title" Value="$Resources:cmscore,IPPT_HomeWelcomePage_Title;" />
        <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/WelcomeLinks.aspx, $Resources:cmscore,PageLayout_WelcomeLinks_Title;" />
        <Property Name="ContentType" Value="$Resources:cmscore,contenttype_welcomepage_name;" />
    </File>
</Module>

As you can see, SubWebWelcome provisions the default.aspx using the WelcomeLinks Page Layout.

If you want a different Page Layout for the default page, you have two options:

  1. Create a custom site definition based on BLANKINTERNET that uses your desired Page Layout.
  2. Continue to use the out of the box site definition with custom code (launched by either feature stapling or event receivers) that changes the Page Layout from WelcomeLinks.
3
votes

You don't need to deploy a custom page layout but you do need to use code. The way we have solved this is to create an Event Receiver for the WebProvisioned event which will fire after a new SPWeb has been created.

What you can do is to update the PublishingPage in the new web with the Page Layout that you want. This allows users to create new webs but you to set the default Page Layout of each new web.

This is the event receiver code:

public override void WebProvisioned(SPWebEventProperties properties)
{
    try
    {
        if (PublishingWeb.IsPublishingWeb(properties.Web))
        {
            PublishingWeb curPubWeb = PublishingWeb.GetPublishingWeb(properties.Web);

            foreach (PageLayout curLayout in curPubWeb.GetAvailablePageLayouts())
            {
                if (curLayout.Name == "DefaultPageLayout.aspx")
                {
                    foreach (PublishingPage curPage in curPubWeb.GetPublishingPages())
                    {
                        curPage.CheckOut();
                        curPage.Layout = curLayout;
                        curPage.Update();
                        curPage.CheckIn("");
                    }
                    break;
                }
            }
        }
    }
    catch (Exception ex)
    {
        /* Handle exception here */
    }
}

And this is the code to register the event receiver (this can be run when your feature is activated or can be run once from a PowerShell script or console application):

using (SPSite topSite = new SPSite("[Site Collection URL]"))
{
    SPEventReceiverDefinition webEventDef = topSite.EventReceivers.Add();
    webEventDef.Name = "Web Adding Receiver";
    webEventDef.Synchronization = SPEventReceiverSynchronization.Synchronous;
    webEventDef.Type = SPEventReceiverType.WebProvisioned;
    webEventDef.SequenceNumber = 4001;
    webEventDef.Assembly = "MyCustomAssembly, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=123456789";
    webEventDef.Class = "MyCustomAssembly.CustomEvents";
    webEventDef.Data = "Adding publishingwebfeatures";
    webEventDef.Update();
}
1
votes

If the Publishing feature is enabled at the site, it should be as simple as going to:

Site Settings, Look and Feel, Welcome Page and selecting the page

0
votes

It seems that when you add publishing sub webs to SharePoint it doesn't seem to inherit the parent webs default page layout. What is more is even if you call SetDefaultPageLayout passing true to reset all sub-sites, this setting still does not stick.

After scaffolding my entire site structure (sub-webs), I had to implement the following recursive function to ensure the top most default page layout is being inherited, hopefully this helps someone.

// Recursively update sub-webs to inherit the default page layout.
Action<PublishingWeb> updateWebRecursive = null;
updateWebRecursive = new Action<PublishingWeb>((parentWeb) =>
{
    PublishingWebCollection childWebs = parentWeb.GetPublishingWebs();
    if (!parentWeb.Web.IsRootWeb)
    {
        parentWeb.InheritDefaultPageLayout();
        parentWeb.Update();
    }
    foreach (PublishingWeb childWeb in childWebs)
    {
        updateWebRecursive(childWeb);
    }
});
updateWebRecursive(pubWeb);