Creating a page layout with web parts added to the page

In our custom page layouts, we can add web parts to the templates in two ways. First, we can add them to a web part zone template. This adds the web part to the web part zone when a page is created with the page layout and they can be modified or removed after the page has been created.

Secondly, we can reference the web part directly on the page outside of web part zones. This will insert the web part onto the page, but it cannot be modified or removed from the page when editing the page in the SharePoint web interface.

Note

Web parts added directly to the page layout are added to the page when a page is created. Adding additional web parts to a page layout will not update content pages already created with the page layout.

In this recipe, we will add two web parts to our page layout (one inside a web part zone template and one outside a web part zone). We will use the page layout we created previously with the three web part zone columns as a starting point.

How to do it...

Follow these steps to create a page layout with web parts added to the page:

  1. Open SharePoint Designer.
  2. Select Open Site. Enter the complete URL to the SharePoint site and select Open.
  3. From the Navigation pane, select Page Layouts.
  4. In the list of files in the Page Layouts library, make a copy of PageLayout_ThreeColumn.aspx (for our example, we have renamed it PageLayout_WebPartsAdded.aspx).
  5. Check out the new PageLayout_WebPartsAdded.aspx page layout.
  6. Open the PageLayout_WebPartsAdded.aspx page layout.
  7. Locate the first <div class="ms-table ms-fullWidth"> element.
  8. Before the <div> elements, add a TableOfContentsWebPart element.
    <PublishingWebControls:TableOfContentsWebPart ID="TableOfContentsWebPart" Title="Table of Contents" runat="server" />

    Note

    Web parts added outside of a WebPartZone ZoneTemplate are added to the page, but are not editable from the SharePoint web interface.

  9. Using the SharePoint table layout styles, add three <div> column containers as shown in the following code:
    <div class="ms-table ms-fullWidth">
    <div class="ms-table ms-fullWidth">
    <div class="cell-margin tableCol-33">
    </div>
    <div class="cell-margin tableCol-33">
    </div>
    <div class="cell-margin tableCol-33">
    </div>
    </div>
    <SharePointWebControls:ScriptBlock runat="server">
    if(typeof(MSOLayout_MakeInvisibleIfEmpty) == &quot;function&quot;) 
    {MSOLayout_MakeInvisibleIfEmpty();}</SharePointWebControls:ScriptBlock>
    </div>
  10. In ZoneTemplate of one of the WebPartZone elements, add a MediaWebPart as shown in the following code:
    <WebPartPages:WebPartZone runat="server" Title="<%$Resources:cms,WebPartZoneTitle_Center%>" ID="CenterColumn">
    <ZoneTemplate>
    <PublishingWebControls:MediaWebPart ID="MediaWebPart" Title="Media Web Part" runat="server" />
    </ZoneTemplate>
    </WebPartPages:WebPartZone>

    Note

    Web parts added in a WebPartZone ZoneTemplate are added to the page when created and are editable from the SharePoint web interface.

  11. Save the page layout.
  12. Navigate back to the Properties page for the page layout.
  13. Under Customization, select Manage all file properties in the browser.
  14. Select Edit Item from the ribbon.
  15. Ensure that the Content Type option is set to Page Layout.
  16. Provide a new title for the page layout (for example, Web Parts Added).
  17. In the Associated Content Type option, set Content Type Group to Page Layout Content Types and Content Type Name to Article Page.
  18. Save the item.
  19. Check in and publish the page layout using the Check In and Publish options.
  20. Navigate to the Pages library of the site using your preferred web browser.
  21. Select New Document from the FILES tab on the ribbon.
  22. Provide a title and URL for the new page in the Title and URL fields.
  23. Select the newly created page layout.
  24. Select Create.
  25. Edit the new page to observe our new layout and web parts. The following screenshot shows the page where we can edit our web parts:
    How to do it...

How it works...

Web parts are ultimately ASP.NET server controls at their core. As such, they can be added directly to a page layout like any other server control. Doing so puts them on the page, but does not let users edit them in the web interface.

Web part zones are server controls that provide a section of the page that users can add web parts to in the web interface. They also have a template that allows a page layout to include web parts in the zone by default when the content page is created. These web parts can be edited in the web interface after the content page is created.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236