How to add a web part page to a site definition?

14,782

Solution 1

You can follow this methodology which uses Feature Stapling. I used this to automatically add web parts to My Sites when they are created:

http://blogs.msdn.com/sharepoint/archive/2007/03/22/customizing-moss-2007-my-sites-within-the-enterprise.aspx

Solution 2

You can provision the page in ONET.XML.

First add a web part page template to your site definition.

Then provision an instance of the page (with the web parted added) in your ONET.XML.

This stuff is described fully in Ted Pattison's book Inside Windows SharePoint Services 3.0

default.aspx

<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page language="C#" MasterPageFile="~masterurl/default.master"    
          Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage" %>

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <table cellspacing="0" border="0" width="100%">
      <tr>
       <td class="ms-pagebreadcrumb">
            <asp:SiteMapPath SiteMapProvider="SPContentMapProvider" id="ContentMap" SkipLinkText="" NodeStyle-CssClass="ms-sitemapdirectional" runat="server"/>
       </td>
      </tr>
      <tr>
        <td>
         <table width="100%" cellpadding=0 cellspacing=0 style="padding: 5px 10px 10px 10px;">
          <tr>
           <td valign="top" width="70%">
               <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Left" Title="loc:Left" />
               &nbsp;
           </td>
           <td>&nbsp;</td>
           <td valign="top" width="30%">
               <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Right" Title="loc:Right" />
               &nbsp;
           </td>
           <td>&nbsp;</td>
          </tr>
         </table>
        </td>
      </tr>
    </table>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
    <SharePoint:ProjectProperty ID="ProjectProperty1" Property="Title" runat="server"/>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
         <label class="ms-hidden"><SharePoint:ProjectProperty ID="ProjectProperty2" Property="Title" runat="server"/></label>
</asp:Content>

ONET.xml snippet

<Module Name="Default" Url="" >
  <File Url="default.aspx" Type="Ghostable">
    <!-- Add a Web Part to left zone -->
    <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="0">
      <![CDATA[         
       <WebPart 
         xmlns="http://schemas.microsoft.com/WebPart/v2"
         xmlns:cewp="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
         <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
         <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
         <Title>Working with Site Definitions</Title>
         <FrameType>TitleBarOnly</FrameType>
         <cewp:Content>
           This Web Part was added through declarative logic in ONET.XML
         </cewp:Content>
       </WebPart>
       ]]>
    </AllUsersWebPart>
  </File>
</Module>
Share:
14,782
knight0323
Author by

knight0323

I am Jeremy Knight, a software developer and architect in the greater New Orleans area. I consider myself a software craftsman and I am constantly looking to better both my development and management practices. I have a passion for mentoring. I have had the privilege to work with great developers and I enjoy paying their tutelage forward. Since 2011, I have been a speaker and coordinator for local user groups. I am currently coordinator for the Houma-Thibodaux .NET User Group and a volunteer for the Baton Rouge .NET User Group. The user groups are focused on helping local Microsoft developers network with peers and learn more about the technologies they work with daily. I have worked extensively with Microsoft technologies such as C#, ASP.NET, and SQL Server as well as web technologies such as HTML, CSS, JavaScript, jQuery, Bootstrap, Telerik, and more.

Updated on June 05, 2022

Comments

  • knight0323
    knight0323 almost 2 years

    I have to create a site definition for a client that must contain pre-defined web part pages. I can create the web part pages but am at a loss when it comes to attaching them to the site on creation.

    I know web part pages created through SharePoint are stored in a Document Library. Do I need to pre-populate a "Web Part Pages" document library and add the needed navigation to these files? If so, how do I go about adding the needed aspx files?

    Finally, are there any caveats that I should be aware of for configuring the custom web part page in onet?

  • knight0323
    knight0323 over 10 years
    Rob's answer is not what I was looking. What we're trying to do is add additional Web Part Pages that will have pre-defined web part layouts already set up and linked to on either the top or quick nav. I never even thought about using a feature and stapling it! I have no idea why considering we're doing all of our custom lists that way. I'll give it a try! Thanks webwires.