Create SharePoint page instance from a Feature…

Hi all,

Ever needed to create bunch of SharePoint pages with some  content embedded on it? (About us page with some static content inside a rich text field, Contact us page, with a web part embedded on the page?)

I had a requirement like that for a particular project. I had to create a page instance to hold a custom web part, that was created to get user feedback. Also this page instance should be instantiated with a separate feature. So how to handle this requirement.

Because we are going to create a SharePoint feature, we need a Feature.xml file.

<?xml version="1.0"?>
<Feature Id="GUID" Title="My Feature - Create Page Instance" Version="" Scope="Web" xmlns="">
      <ElementManifest Location="ElementManifest.xml" />
      <ElementFile Location="template.aspx"></ElementFile>

As you can see, the feature file reference two other files. (template.aspx, ElementManifest.xml)

template.aspx file will be used as a page template to create a page instance. The content of the file will be like this.

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Reference VirtualPath="~TemplatePageUrl" %>
<%@ Reference VirtualPath="~masterurl/custom.master" %>

The ElementManifest.xml file will be the most important. This file will contain all the information about your new page instance.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="">
   <Module Name="Home" Url="Pages" Path="" RootWebOnly="FALSE">
      <File Name="default.aspx" Url="default.aspx" Type="GhostableInLibrary" Path="template.aspx" IgnoreIfAlreadyExists="TRUE">
         <Property Name="Title" Value="Home" />
         <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/PageLayouts/MyPageLayout.aspx, My Page Layout Name" />
         <Property Name="ContentType" Value="MyPageLayoutContentType" />

This Module will use the template.aspx file as a template and instantiate a page with given attributes. In the above example, the feature will create a page with the name “default.aspx”. And the page title will be set to “Home”. Each and every SharePoint publishing page should have a base page layout associated with it. So for the above page instance it the associated page layout will be “MyPageLayout.aspx” located in the Master pages and page layouts library. Also we have to specify the Content Type associated with the particular Page Layout we selected previously.

Everything OK now to install the feature and activate. The web page will be created in the SharePoint “Pages” document library.

If your page layout contains additional fields, and if you want to set values for those you can define them as a property. Also if you have any custom or OOB web parts, to be included you can specify them in the AllUserWebParts section.

<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
        <WebPart xmlns="" xmlns:iwp="">
           <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
           <Title>Site Image</Title>

Thats all for now :)

Leave a Reply

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