How to Add user control in sharepoint master page

In this post we will see how to add a user Control in a SharePoint Master Page, along with its code file. Here we will create a user control with a hyperlink, and will change the title of the hyperlink and its navigation url in the page load of the code file.
The steps are:

1. Create the Web user Control (ascx) along with the code file.

Open Visual Studio and Create a new web application project.


Add a Web User Control in your project.


In the ascx file add a hyperlink control.( like below)



Now, lets add few lines in our code behind file. See the below image.



Ok we are done adding the stuff. Now lets build the Project. Oh wait! we didn’t sign the project, since we are going to throw the .dll for this project into GAC, we need to sign the project with a key.

2. Signing the Project -

firstly, let create a key that we need for signing. To do this browse to the VS tools command prompt (See below ).



Now, create the key using the command below.



Now build the Project.

After you have done this, we need to make one little modification to the ascx file so that it uses what we have written in the code behind. We usually do not deploy .cs or code files along with our ascx or aspx pages in Sharepoint, instead the ascx file accesses the code file, by inheriting from the .dll of the project. so lets change the Inherit tag in the ascx file to something like below :

Inherits=”MasterPageUserControlDemo.MasterPageUserControl1, MasterPageUserControlDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0745576964bce313″

Now build the Project once again.

3. Deploy the Control -

To add the control in SharePoint context, Open the project folder and just drag and drop the control into controltemplates folder of SharePoint 12 hive.

I mean add this under this path : “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES”

Next, drag and drop your signed assembly into GAC or C:\WINDOWS\assembly folder.

4. Now, we are ready to add our user control in our master page -

So, lets start with registering the user control in the master page . To do this, open your SharePoint site in SharePoint designer and check out the master page. Now, add the line below

on the top the page. See the Image below.

<%@ Register TagPrefix=”ouruc” src=”~/_controltemplates/MasterPageUserControl1.ascx” TagName=”OurSitesLink” %>



Now lets just add the control anywhere on your page. I added the below line in the footer of the page . See the image below.

>ouruc:OurSitesLink id=”Footerlink” runat=”server”/<



Now check in the page and approve it.

The Result should be something like below.