Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘master page’

How to Deploy JavaScript to Master Page through Solution?

Posted by Jean Paul on October 2, 2013

In this article I would like to take you through a real-world problem. You are working on a Java Script method which should be deployed to multiple SharePoint servers. The Java Script needs to be included in the Master Page file.

How to deploy it?

The manual deployment requires going to each SharePoint site, Opening the page in SharePoint Designer & Editing the master page. This way is time consuming & has maintenance overheads.

How to Automate Deployment?

We need to automate the deployment process. We can use the AdditionalPageHead delegate to render our custom code to master page & everything through automated deployment.



Please follow the steps below to achieve the following:

ยท Deploy Java Script page to SharePoint Master Page

Step 1: Create new project

Open Visual Studio & Create an empty SharePoint project & name it as MasterPageCode. Choose the farm solution option.


Step 2: Add User Control

Add a User Control item to the project & name it as MyControl. We are going to place our JavaScript code here.


In the appearing user control, place the following code which says Hello World through JavaScript.

alert(‘Hello World’);

The user control looks like below:


Now we need to include this user control code in the Master Page. We can achieve this in the next step.

Step 3: Add Deployment Element

Choose Add new item & select the Empty Element option & name it as MyAction.


Replace the content of file with following code. (please ensure the folder & file names are correct)

<?xml version=”1.0″ encoding=”utf-8″?>

<Elements xmlns=””&gt;

<Control Id=”AdditionalPageHead” Sequence=”150″




Now the Solution Explorer looks like below:


Step 4: Deploy Solution

Now right click on the solution & choose the Deploy option. Wait for a few seconds & your solution will get deployed & activated automatically.


During runtime, the Java Script code will be appended to Master Page.

Step 4: Testing Solution

Open the default SharePoint site & you should receive the following JavaScript message box.



I thought of adding some important points:

1. The default master page of SharePoint pages is default.master & it resides in the 14 Hive > Templates > Layouts folder.

2. The AdditionalPageHead tag ensures the user control code is added to the master page. We can have multiple user controls rendered in this way.

3. The MyAction.xml file ensure the user control is deployed through the WSP file.



In this article we have explored a real world scenario. I hope the code will be useful in real word scenarios of integrating Java Script or Custom Controls to every page through solutions.

I have attached the source code along with.

Posted in SharePoint 2010 | Tagged: , , , , , , , | Leave a Comment »