Including page layouts in a SharePoint solution

Page layouts included in a SharePoint solution require additional data to be added to their library properties to associate them with the page layout content type. In this recipe, we will include a copy of the BlankWebPartPage.aspx page layout that we have renamed Tacoma_BlankWebPartPage.aspx with the appropriate properties. You can download a copy of BlankWebPartPage.aspx from the web interface or with SharePoint Designer.

How to do it...

Follow these steps to include a page layout in the solution:

  1. From the Solution Explorer pane in Visual Studio, right-click on the project name.
  2. Click on Add and then select New Item.
  3. Navigate to Visual C# Items | Office/SharePoint and select Module.
  4. Give a name to the Module (for example, PageLayouts).
  5. Click on Add.

    Tip

    Since both master pages and page layouts are added to the same SharePoint library, they could both exist in the same module.

  6. Delete the Sample.txt file in the PageLayouts module.
  7. Right-click on the PageLayouts module.
  8. Select Add and then select Existing Item.
  9. Select the Tacoma_BlankWebPartPage.aspx page layout from your filesystem.
  10. In the PageLayouts module, open the Elements.xml file.
  11. On the <Module> element, set the Url property to _catalogs/masterpage, the Path property to null, and the RootWebOnly property to FALSE.
    <Module Name="PageLayouts" Url="_catalogs/masterpage" Path="" RootWebOnly="FALSE">
  12. On the <File> element, set the Type property to GhostableInLibrary, the IgnoreIfAlreadyExists property to TRUE, and the Url property to Tacoma_BlankWebPartPage.aspx.
  13. In the <File> element, add the properties that will be assigned to the file in the SharePoint library.
    <File Path="PageLayoutsTacoma_BlankWebPartPage.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" Url="Tacoma_BlankWebPartPage.aspx">
    
    <Property Name="Title" Value="$Resources:Code6587ENCh04,Tacoma_PageLayout_Name;" />
    
    <Property Name="Description" Value="$Resources:Code6587ENCh04,Tacoma_PageLayout_Description;" />
    
    <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
    
    <Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#"/>
    
    </File>
  14. Save the Elements.xml file. The following screenshot shows the contents of the Elements.xml file:
    How to do it...

How it works...

In addition to adding files to a SharePoint library, a module can also set the properties of the file in the library. In this recipe, we added a custom page layout to a module and set the properties of the file to provide it with a page layout title and associate it with the page layout content type.

The values of the Title and Description properties are resource strings that we will create in the Adding localization to a SharePoint solution recipe. The ContentType property uses a resource string from SharePoint and the PublishingAssociatedContentType property instructs SharePoint to make this file a page layout.

The /_catalog/masterpage library, where master pages and page layouts are stored, contains multiple content types. If a page layout file is not associated with the page layout content type, it will not be available in the web interface when creating a new content page.

See also

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

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