0

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

0

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

  • Dear @SagirKazi , take your time to test this solution and Please don't forget to set this as answer in case it's solved your issue and also upvote it if it's useful to you , I am also here if you need more clarification :) – Mohamed El-Qassas MVP Aug 2 '16 at 19:56
  • I did verify that my custom masterpage is deployed correctly and that the right path (all variations) is mentioned in the Page directive in Page Layout but still no progress. – Sagir Kazi Aug 3 '16 at 16:31
  • should you provide me with the path that master page has been deployed on it ? – Mohamed El-Qassas MVP Aug 3 '16 at 16:33
  • Below is from the Elements.xml file for MasterPages. <Module Name="MasterPages" Url="_catalogs/masterpage" Path="MasterPages" List="116" RootWebOnly="TRUE"> <File Path="HomeResponsive16.master" Url="HomeResponsive16.master" ReplaceContent="TRUE" Type="GhostableInLibrary" Level="Published" IgnoreIfAlreadyExists="TRUE"> <Property Name="Title" Value="HomeResponsive16 MasterPage"></Property> <Property Name="UIVersion" Value="15" /> <Property Name="MasterPageDescription" Value="" /> </File> </Module> – Sagir Kazi Aug 3 '16 at 16:36
  • Because Comments are not for extended discussion kindly join this room chat.stackexchange.com/rooms/43413/… – Mohamed El-Qassas MVP Aug 3 '16 at 16:56
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.