In the previous article, we discussed creating a custom master page in SharePoint Online using HTML, CSS, and Design Manager. In this article, we will create a page layout. Page layouts can be thought of as a customized layout of contents in SharePoint. The contents may include placeholders for Web part zones, image galleries, etc.
The concept is the same as before and revolves around these high-level steps.
- Create a new page layout using Design Manager
- Make the modifications in the Page Layout as per requirement
- Get a code for inserting web part zones
- Edit any page to use the new page layout
Our goal is to create a page layout that will look like this.
Follow along with the steps to get it completed!
Step 1. Create a new page layout using Design Manager.
Navigate to your design manager from the settings icon on your site. Once it opens, click on “Edit Page Layouts” available on the right-hand side for quick launch.
Click on “Create a Page Layout”.
Fill in the details on the next screen. The following information is needed.
- Name: The name of the Page layout that we are creating (SPSPageLayout in our case)
- Master Page: The parent master page where the Page layout will be associated. (spsindustries)
- Content Type: The content type under whose listing our newly created template will appear(Articles in our case)
Clicking on OK will redirect you to the page creation confirmation page as shown below.
Notice that the page layout is still in draft state. We will complete it very soon. Follow along with the next steps.
Step 2. Make the modifications in the Page Layout as per requirement.
If you click on the “Conversion Successful” link of the previous screen (shown above) you will be redirected to the Preview page of the page layout. By default, it contains some existing elements as placeholders (Page Image, Page Content, etc.)
Now, to make our modifications we will have to make use of SharePoint Designer (SPD). Open the site in SPD, and navigate to the “Page Layouts” section in the right-hand quick launch bar,
The concept is the same as editing Master pages – edit only the HTML part and the ASPX page will be refreshed with updates. Next, check out the HTML page (SPSPageLayout.html) and click on “Edit File in Advanced Mode”. This will open the file in SPD.
It can be noticed that the existing elements/placeholders that come along with the page layout by default are placed in “divs” inside the HTML, as seen below. The idea is to get rid of these elements and then include our own HTML. For inserting SharePoint components in our page layouts, we will make use of “Snippets”. We will build our page layout, something of this sort.
Now, after removing the default SharePoint Components we go ahead and write down our own custom HTML inside the page layout. Make sure that all your HTML is written inside the ASP content placeholder with the ID “PlaceHolderMain”. The screenshot below depicts the same (removing existing SharePoint components and where to write our custom HTML)
Insert a small HTML table snippet as shown below in the space reserved on the previous screen. Think of the HTML table as the container for inserting our web part zones. Here is the HTML below,
<p style="color:red">
<b>SPS Industries Communities Services</b> - We actively look for Volunteers interested in lending a helping hand
</p>
<table style="width:90%; min-height:300px">
<tr>
<td rowspan="2" style="border:2px solid red"> Space for Webpart Zone 1 </td>
<td style="border:2px solid green"> Space for Webpart Zone 3 </td>
</tr>
<tr>
<td style="border:2px solid green"> Space for Webpart Zone 3 </td>
</tr>
</table>
Step 3. Get code for inserting web part zones.
Click on the Preview page as shown in Step 2, and click on the “Snippets” link present in the right-hand top corner.
Once the Snippet page opens, click on “Web Part Zone” as shown in the image below. On clicking the link, the HMTL snippet will be generated with a unique ID (every time you click on the “Web Part Zone” a new unique ID is generated and integrated with the HTML snippet).
On a side note, there are a whole bunch of snippets available in the Snippets gallery. Do explore the other types. The usage is the same for every type of them. Select the snippet type, and a new code of HTML snippet will be generated, which can then be copy-pasted and used in your SPD.
Copy the HTML snippet and paste the entire code in the HTML table’s first cell (i.e. inside the first <td> block replacing the “Space for Web part Zone 1”.
Repeat this step 2 more times. Make sure that you click on Web Part Zone and the page refreshes with a new ID before copy-pasting the HTML snippet.
The code will look similar to this.
On completion of the code, save the page, check in, and then Publish a major version.
Step 4. Edit any page to use the new page layout.
Now - all the heavy lifting has been done, and it is time to reap the benefits of your hard work.
Go ahead and click on the “Edit” page. It can be noticed that the SPSPageLayout can be found under the Articles content type. Select that and see the page change its layout to the one you’ve created!
There you go! The screen above shows how a page looks using the newly created customized page layout.
This article can also be found in my personal blog – SharePoint Surgeon.