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
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"?>
<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
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.