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="1.0.0.0" Scope="Web" xmlns="http://schemas.microsoft.com/sharepoint/">
   <ElementManifests>
      <ElementManifest Location="ElementManifest.xml" />
      <ElementFile Location="template.aspx"></ElementFile>
   </ElementManifests>
</Feature>

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=12.0.0.0,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="http://schemas.microsoft.com/sharepoint/">
   <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" />
      </File>
   </Module>
</Elements>

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">
     <![CDATA[
        <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image">
           <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
           <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName>
           <FrameType>None</FrameType>
           <Title>Site Image</Title>
           <iwp:ImageLink>/_layouts/images/homepage.gif</iwp:ImageLink>
        </WebPart>
      ]]>
  </AllUsersWebPart>

Thats all for now 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s