Provisioning Publishing Pages

By | 2010-08-23

On the interweb there seems to be some confusion on how to provision publishing resources through features.
Here is what works for SharePoint 2010.
First you wil need a Feature scoped for Site (SPSite), put your ContenType module here.
And a Feature scoped for Web and add the other modules here!

1. Create a Page ContentType

Add a New ContentType to your project. VS 2010 will ask you from which ContentType you want to inherit, select Article Page.
You will get a new ContentTypeId!
Supply a Name, Group and Description for your Page ContentType.

Add the (Publishing) Fields you want to use in your Publishing Pages.

2. Provision a MasterPage

Now add an empty Module, name it MasterPages and remove the sample.txt.
Add your Site.Master page to your newly created module
Open the Element.xml of your module
Apply the following changes:

  • Add RootWebOnly=”TRUE” to you Module
  • Add a Title to your File
  • Add a Description to your File
  • Add a ContentType to your File with the value $Resources:cmscore,contenttype_masterpage_name;, cause this is a masterpage!

3. Provision a Publishing PageLayout

Add your PageLayout to either a new Module or the MasterPage Module. If you create a new Module, don’t forget to add the RootWebOnly=”TRUE”.

You can also add “default” WebParts to you page layout with the AllUsersWebPart tag.

4. Provision a Publishing Page

And you can even provision Publishing Pages with a module.
Now add an empty Module, name it DefaultPages and remove the sample.txt.
Open the Element.xml of your module
I have provided a sample here, please note the following:

  • Use the ContentTypeBinding to bind your CustomPageContentType to the Pages list
  • Use the default.aspx from the default SharePoint Site Definition SPS by adding the SetupPath attribute to your Module (more here).
  • You can provision WebParts and Field Values
  • To deploy multiple Publishing Pages add another File ellement, and change the Url attribute of the File Element, to create another page.
  • You can use the feature to provision Pages from within you Onet.xml, but don’t forget to activate dependand features first ( Publishing Infrastructure, Publishing, optional ContentTypes )

5. Small bonus tip

When you deploy MasterPages and PageLayouts through features, they become part of the site collection. You provision these files in the _catalog/MasterPages SPList of your Site Collection! Now default SharePoint won’t load the contents of your deployed masterpages and pagelayouts into the actual ContentDb. It only uses a reference in the ContentDb to the actual file on Disk in your Feature folder. So updating these PageLayouts and MasterPages can be done by uploading a new Solution with the updated Files.
But user’s can change files in the Site Collection through SharePoint Designer. If a User Customizes a MasterPage or PageLayout, it becomes Customized or Un-Ghosted. This means the file contents are now stored in the ContentDb! And when you re-deploy your solution with your new feature and files (update the feature version number!) you would expect to see your changes, but you won’t!

You can use the object model to undo this, Re-Ghosting, by calling the RevertContentStream() on the SPFile of your PageLayout or MasterPage.
I have written a method for that which you can call from the Feature Event Receiver Activate and Upgrade (version number of your feature 😉 ):

2 thoughts on “Provisioning Publishing Pages

  1. Pingback: Sharepoint 2010 Module key: IgnoreIfAlreadyExists « Alen Ziver Malhasoglu's Microsoft Commerce Server Blog

  2. aeron

    This is nice. 🙂

    How about adding multiple pagelayouts in our site defitinition?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *