3
votes

To simplify our SharePoint environment I am wanting to have a central and single location where all of the master pages are stored so I was hoping to reference the masterpage files directly from the _layouts directory.

I add the files to the "\LAYOUTS\MyMasterPages" directory and then update the SPWeb object to use that path:

web.MasterUrl = "/_layouts/MyMasterPages/newdefault.master"

But when I go to the site I get:

The referenced file '/_layouts/MyMasterPages/newdefault.master' is not allowed on this page. at System.Web.UI.TemplateParser.ProcessError(String message) at System.Web.UI.BaseTemplateParser.GetReferencedType(VirtualPath virtualPath, Boolean allowNoCompile)
at System.Web.UI.PageParser.ProcessMainDirectiveAttribute(String deviceName, String name, String value, IDictionary parseData) at System.Web.UI.TemplateParser.ProcessMainDirective(IDictionary mainDirective)

I am assuming that this may be due to trust level security policies and a CAS configuration but I am not entirely sure. Is there a way to allow this central way of using master pages across the enterprise or am I stuck using the /_catalog/ virtual path?

3

3 Answers

3
votes

This worked for me:

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    this.MasterPageFile = "path to masterpage";
}

as proposed on: How to use the site's masterpage from a _layout SharePoint page?

0
votes

My understanding (but I don't know why) is that Master Pages need to be in the MasterPage gallery of each site collection.

Since you would need to deploy those packages to each SharePoint server (frontend, application, database, etc) in the farm, I think the requirement is either related to clustering, or perhaps to the database and ghosting.

In either case, be sure to use custom features as the way you deploy your master pages regardless of your farm size.

I understand the want to do things your own way with SharePoint, and while I am normally on board with that kind of thinking, the past year of pain has taught me that living inside of the recommended SharePoint boundaries is the best way to go.

0
votes

The master page used by a SharePoint site must be located in the master page gallery. This is a hard requirement by SharePoint as far as I know.

This gives you more benefit than you might think. This allows you to change the master page in realtime using SharePoint designer with the changes only visible to you. Once the master page is check-in and published as a major version, the change becomes visible to the public. You also have version history on the master page.

I understand you pain, I have been in the same situation as you are. What I learned is: don't fight the platform, just work within it.