Question

I am working on custom branding using Visual Studio for SharePoint 2016 On-prem environment.

I got 2 masterpages (HomeResponsive16.master and Responsive16.master) and few page layouts. By default, Responsive16.master is set on the site. I want couple of page layouts (for instance Home-2Col.aspx) to use the other masterpage (i.e. HomeResponsive16.master) and not the default one.

My Home-2Col.aspx page layout's Page directive looks like this:

<%@ Page language="C#" MasterPageFile="HomeResponsive16.master" CodeBehind="Home-2Col.aspx.cs" Inherits="ResponsiveToolkit._16.PageLayouts.Home_2Col, $SharePoint.Project.AssemblyFullName$" %>

When I create a page (defaultnew.aspx) using the Home-2Col page layout, it still loads using the default master page rather than the one I set in the Page directive.

I also tried adding the code-behind for the page layout and setting the master page on the PreInit event but even that didn't work. I also confirmed that the SafeControl entry is present in the web.config for the code-behind.

Below is how my VS Solution looks like.

enter image description here

Was it helpful?

Solution 2

This is how I solved this.

My Home-2Col.aspx page layout's Page directive looks now like this:

<%@ Page language="C#" CodeBehind="MyPageLayout.cs" Inherits="Toolkit.PageLayouts.MyPageLayout, $SharePoint.Project.AssemblyFullName$" %>

Added a codebehind file (MyPageLayouts.cs) under PageLayouts and added the below code to set the masterpage.

namespace Toolkit.PageLayouts
{
    public class MyPageLayout : Microsoft.SharePoint.Publishing.PublishingLayoutPage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            this.MasterPageFile = "HomeResponsive16.master";
        }
    }
}

Hope this helps!

OTHER TIPS

I think the main issue at your masterpagefile path at Page directive, so make sure that the new master page has been deployed correctly to http://rootsite/_catalogs/masterpage/ then try to change the path file at Page directive as the following

masterpagefile="../_catalogs/masterpage/HomeResponsive16.master"

[Update]

The above suggestion is for a Sharepoint Page, But when I read the quest again he talks about page layout.

the Page Layout in SharePoint allows you to create a template for a page that users apply to their content for a consistent layout of information.

So you can develop your page layout using VS and set it as a template to SharePoint page and at SharePoint page you can set its custom Master Page.

You can read more about page layout here

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top