Provisioning Publishing Site with Custom Content Types and Page Layouts

Today I was writing a feature to provision the Pages and Documents library with my custom content types and the Pages library has a Default.aspx based on a custom Page Layout. All this should be done when a new site was created based on a custom site definition with the publishing features activated.

In my project I added a new module and named it ProvisionSite:

ModuleProvisionSite

I removed the Sample.txt and added a default.aspx.

To add the default.aspx to the Pages library I edited the Elements.xml file:

  <Module Name="ProvisionBUSite" Url="$Resources:osrvcore,List_Pages_UrlName;" Path="">

    <File Path="ProvisionSitedefault.aspx" Url="default.aspx" Type="GhostableInLibrary">

      <Property Name="Title" Value="$Resources:cmscore,IPPT_HomeWelcomePage_Title;" />
      <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/OrdinaWelcomePage.aspx" />
      <Property Name="ContentType" Value="Ordina WelcomePage" />

    </File>

  </Module>

That’s all for the module. A feature named Feature1 is also created. I renamed this feature to ProvisionSite and opened the designer:

FeatureProvisionSite

That’s just fine. I added an EventReceiver to provision the content types.

FeatureAddEventReceiver

Uncommented the FeatureActivated method and I coded this:

        public override void FeatureActivated(SPFeatureReceiverProperties properties) {

                SPWeb newWeb = Feature.GetActivationWeb(properties);

                //Pages Library
                SPContentType ordinaWelcomePage = newWeb.AvailableContentTypes["Ordina WelcomePage"];
                SPContentType ordinaArticlePage = newWeb.AvailableContentTypes["Ordina ArticlePage"];

                SPList list = newWeb.Lists[PublishingWeb.GetPagesListName(newWeb)];

                list.ContentTypes.Add(ordinaWelcomePage);
                list.ContentTypes.Add(ordinaArticlePage);

                list.Update();

                //Documents Library
                SPContentType ordinaDocument = newWeb.AvailableContentTypes["Ordina Document"];

                string listName = SPUtility.GetLocalizedString("$Resources:cmscore,ListNameDocuments", "cmscore", newWeb.Language);

                SPList documentsLib = newWeb.Lists[listName];

                documentsLib.ContentTypesEnabled = true;
                documentsLib.ContentTypes.Add(ordinaDocument);

                documentsLib.Update();

}

 

2 special lines of code I want to point out:

line 8: PublishingWeb.GetPagesListName

line 18: GetLocalizedString()

Both return the correct name of the Pages and Documents library no matter what language is used. Especially the Documents library was hard to find. Do not mistake this library with Shared Documents (that’s the team site version). To get the right name I was googling around. I found a post, but this was the wrong resource file and name. So, I had to find it myself in the SharePoint Root folderResources. And there she was: cmscore.resx. :p Gotcha! The rest was easy….

You have the option to activate the feature manually or have it activated automatically. I did latter… 🙂 I also made the feature hidden for users.

I have the custom site definition and in my ONet.xml I have configurations with only features. I just added my newly created feature. In the feature manifest XML file you can see what the ID is. You will need that one.

So, here is some XML from my ONet file:

    <Configuration ID="1" Name="Ordina Site" MasterUrl="_catalogs/masterpage/v4.master">
      <Lists>
      </Lists>
      <SiteFeatures>
      </SiteFeatures>
      <WebFeatures>

        <!-- Lots of default features here (removed it for readibility) -->

        <!-- Ordina Site - Provisioning -->
        <Feature ID="0049aa68-be5f-4748-bbc9-c4863e2c41d2"></Feature>

      </WebFeatures>

    </Configuration>

Now, that was fun!

Share