Adding Web Parts on the MOSS 2007 Publishing Page Layouts

There are multiple ways you can add web parts on the publishing content pages through the publishing page layouts.

Option 1 – Add only web part zones in the page layouts and let end users to add the web parts in the web part zones
This option will allow developers (through Visual Studio) or designers (through SharePoint Designer) to determine the area where the content authors can add/edit/delete/customize the web parts in the edit mode.

To add the web part zones on the page layouts, fiirst add the directive to add the Microsoft.SharePoint.WebPartPages namespace.

   1:  <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Next step is adding the web part zones with appropriate properties on the page layouts.

   1:  <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
   2:      <div id="contentarea">
   3:           <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" ID="WebPartZone1"
   4:  FrameType="TitleBarOnly" Title="TopMainZone" Orientation="Vertical">
   5:               <ZoneTemplate></ZoneTemplate>
   6:           </WebPartPages:WebPartZone>
   7:      </div>
   8:  </asp:Content>

Option 2 – Add pre-defined web parts in the web part zones defined on the page layouts
This option allows developers or designers to not only define the area where content authors can add/remove/edit web parts but also allows them to add pre-defined web parts with pre-defined properties for the content authors. Best use case for this option is to force the content authors to use the web parts as a field controls. Because this option is more restrictive, content authors cannot delete web parts from the content pages while editing the page.

To add the pre-defined web parts in the page layouts, first add the directive to add the Microsoft.SharePoint.WebPartPages namespace.

   1:  <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Next step is to add the reference for the web part assembly in the page layout file.

   1:  <%@ Register tagprefix="MyWebPartTag" namespace="MyWebPartNameSpace" assembly="MyWebPartAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f131a624888eeed" %>

Last step is to add the web parts in the specific web part zones in the page layout.

   1:  <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server"> 
   2:     <div id="contentarea">
   3:        <WebPartPages:WebPartZone ID="WebPartZone1" runat="server" Title="TopMainZone" PartChromeType="None">
   4:           <ZoneTemplate>
   5:               <MyWebPartTag:MyWebPart ID="MyWebPart1" runat="server" PartOrder="1"></MyWebPartTag:MyWebPart> 
   6:           </ZoneTemplate>
   7:        </WebPartPages:WebPartZone>
   8:     </div>
   9:  </asp:Content>

Andrew Connell also described a method to add pre-defined default web parts in the new content pages based on page layouts using the <AllUsersWebPart> tag while provisioning the page layouts through the feature. You may want to ponder on this option as well.
http://www.andrewconnell.com/blog/archive/2007/10/07/Having-Default-Web-Parts-in-new-Pages-Based-Off-Page.aspx

Advertisements
This entry was posted in SP2007 DEV. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

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