As stated in an MSDN article, “Publishing in Microsoft SharePoint Server 2010 is fundamentally a means of authoring and displaying information using a set of templates.” An Office article adds: “Each publishing page is associated with a page layout. The page layout controls the look and feel of the publishing pages that are created from it. It also defines the fields where contributors can enter various types of article content, such as article titles, graphics, quotations, and unstructured text.” Creating a custom page layout using SharePoint Designer is very straightforward, presenting users with a “what you see is what you get” UI, but using Visual Studio offers its own distinct advantages to creating custom page layouts.
How do I create a custom page layout for a Publishing Site using Visual Studio 2010?
I. Create a new content type
Your new content type must inherit from the Page content type.
Open Visual Studio 2010 (which features integrated Developer Tools for SharePoint 2010) and create a new Empty SharePoint Project:
Click OK when you finish naming your project, and remember to select your deployment type as farm solution.
Next, right-click on your project name in the Solution Explorer window, then click Add -> New Item. In the Add New Item dialog, choose the Content Type project item template:
Click Add when you finish.
The SharePoint Developer Tool will ask which parent content type your content type derives from. Choose the Page content type:
The SharePoint Developer Tool will generate an element.xml file which contains your page content type definition. Note that your content type ID was generated the SharePoint Developer Tool based on its parent’s ID, so it should be kept as is.
Next, add some site columns to your element.xml file (these will be used your page content type).
See my example:
In my example, I have four site columns which are referenced four FieldRef elements inside ContentType element. What you should note here is the FieldFef’s ID and Name should match the Field’s ID and Name (the site columns ID and Name)
Note: To generate a GUID, you can use the GUID Generator tool in Visual Studio 2010 (go to Start -> All Programs -> Microsoft Visual Studio 2010 -> Microsoft Windows SDK Tools -> GUID Generator).
II. Create a custom page layout
It’s an application page (.aspx) which will be used as a page template in a Publishing site.
To create one, right-click on your project name in the Solution Explorer window, then click Add -> New Item. In the Add New Item dialog, choose the Model project item template:
Click Add when you’ve finished.
The SharePoint Developer Tool will generate two files: element.xml and Sample.txt. Rename Sample.txt as an aspx page (my example is PublishingPageLayout.aspx).
Your project structure should now look like the image below:
Next, open your .aspx file and add content to it:
Your page layout should inherit the PublishingLayoutPage class which is contained in namespace Microsoft.SharePoint.Publishing.WebControls in the assembly Microsoft.SharePoint.Publishing. Remember to add a reference to Microsoft.SharePoint.Publishing in your project. It is found at C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI. The controls, such as SharePointWebControls (TextField) and PublishingWebControls (RichHtmlField), should reference the site columns defined in your page content type.
Next, open your element.xml in your Model folder and add a File element inside the Module element to refer to your page layout:
The module needs to be configured to provision the file as content in the library, so we set the File’s Type attribute to GhostableInLibrary.
The Property elements are used to specify the values of certain metadata fields in the Master Page Gallery.
Note: The property, with name PublishingAssociatedContenttype, should have its Value refer to your page content type. Its value is separated character ;# (first part is content type name, and second part is content type ID).
That’s all there is to it. Now, build your project and deploy it to your farm servers (or press F5 to test and debug the project on your selected Web application).
III. Using custom page layout
To use your custom page layout in a Publishing Site, first activate the feature at your site collection.
Go to Site Actions -> Site Settings -> Site collection features (under Site Collection Administration section):
Then, create a Publishing Site (if you don’t have one) going to Site Actions -> New Site:
Note: If you can’t find the Publishing Site template, go to your Site collection features and activate the SharePoint Server Publishing Infrastructure feature:
Your publishing site should look like the image below:
Click on the Page tab –> Edit -> Page Layout:
Then, select your page layout to apply in your publishing site as a page template.
It goes without saying that the benefits of using Visual Studio over SharePoint Designer to build your projects include features such as: reusable code, multiple deployment targets (development, testing, staging, and production environment), and more.
Hope this helps!
- SharePoint Page Layouts Model (ECM) [MSDN]
- SharePoint Field Controls and Control Templates (ECM) [MSDN]
- SharePoint Page Processing Model (ECM) [MSDN]
re: SharePoint 2010 Cookbook: How to Create a Custom Page Layout for Publishing Sites Using Visual Studio 2010
Hey thanks for this it works great.
Just want to share my experience for anybody following this. Be aware that at some points it references MyModel and in other places MyModule so you have to pick one and use it throughout for the example to work.
Once again thanks