SharePoint 2013 custom master page with code behind

Sometime we need to set control value dynamically on our custom master page in SharePoint. The one way is to write custom JavaScript code using Client Side Object Model (CSOM) and we can add that JavaScript on master page.  The other way to create custom class file and attach that file with master page.

Follow below steps to create master page with code behind in SharePoint Project.

1) Create module file in SharePoint solution. I have given module name as CustomMasterPage.


2) Rename the sample.txt with “MyCustomMaster.master”. Add your master page content in “MyCustomMaster.master” file.

3) Now click on module(CustomMasterPage) and add new class file. My class file name is “CustomCode.cs”


4)Inherit that class from publishingpagelayout class.

public partial class CustomCode : PublishingLayoutPage
protected void Page_Load(object sender, EventArgs e)

protected void btnHomePageOk_Click(object sender, EventArgs e)


5)Open the master page and add following line at the top.
<%@Master language=”C#” Inherits=”TestProject.Branding.MasterPages.CustomMasterPage.MyCustom,TestProject, Version=, Culture=neutral, PublicKeyToken=ca7bafa503889ee6″ %>

In above line give change namespace as per your need with master page reference, change assembly name as per your need and give public key token.

SharePoint Error: This page allows a limit of 200 controls, and that limit has been exceeded

I have created a custom page layout through visual studio 2013 on which there are a large number of controls are registered. The page that I had created through the page layout on that page I faced this max control limit issue. Generally , when working with a SharePoint master page, page layout, forms we might will face this type of issue.

The problem was we have a large number of controls registered. The limit is set in the web.config file. By Default controls limit is set by 200.

To resolve this issue open the web.config file, located in a virtual directory in c drive. Find the safe control block as shown below and increase the “MaxControls” limit and do IISRESET.

<SafeMode MaxControls=”200″ CallStack=”false” DirectFileDependencies=”10″ TotalFileDependencies=”50″ AllowPageLevelTrace=”false”>