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.

Get top level parent of Term in SharePoint Taxonomy using C#

While working with SharePoint taxonomy sometimes we come across the problem where we want to get the top level parent of the given TermId. So here i am sharing the way of getting the top level parent term using given Term Id using c# code.

For the implementation of the code you will need to include Microsoft.SharePoint.Taxonomy library.

The below given code you can keep on any event action when you want to get the parent term.

HttpContext oldContext = HttpContext.Current;
HttpContext.Current = null;
string TermId = "Your Term Id"
Term ParentTerms = null;
using (SPSite site = new SPSite("Your site URL"))
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores["Your Managed MeataData Service Name"];
Group group = termStore.Groups["Your Termstore Group Name"];
TermSet sectionTermset = group.TermSets["Your Termset Name"];
ParentTerms = getParentTerm(sectionTermset, TermId);

/* “ParentTerms” is the object of your term, so using this you can get the details of the term like name, Id etc
catch (Exception ex)
// your catch code

Here the method “getParentTerm()” is called recursively to get the top level parent. The definition for the method is as below:-

public Term getParentTerm(TermSet termSet, string termGuid)
Term parentSectionTerm = null;
Term newParentSectionTerm = null;
parentSectionTerm = termSet.GetTerm(new Guid(termGuid));
var isTopLevelParent = parentSectionTerm.IsRoot;
if (!isTopLevelParent)
newParentSectionTerm = parentSectionTerm.Parent;
getParentTerm(termSet, Convert.ToString(newParentSectionTerm.Id));
newParentSectionTerm = parentSectionTerm;
return newParentSectionTerm;

Here “IsRoot” is the property of the term which returns true/false, if the term does not have any parent then it will return ‘true’, and if the term has the parent then it will return ‘false’.

I hope this article will help you to get rid of your problem for getting top level parent of the Term

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”>