.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to Deploy JavaScript to Master Page through Solution in SharePoint

Posted By:Jean Paul       Posted Date: June 30, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

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.
 

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.

clip_image002

Steps

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.

clip_image004

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.

clip_image006

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

 

<script type="text/javascript">
alert('Hello World');
</script>

The user control looks like below:

clip_image008

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.

clip_image010

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="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead" Sequence="150"
  Controlsrc="~/_CONTROLTEMPLATES/MasterPageCode/MyControl.ascx">
  </Control>
</Elements>

Now the Solution Explorer looks like below:

clip_image012

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.

clip_image014

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.

clip_image016

clip_image018

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.

References

http://msdn.microsoft.com/en-us/library/ms470880(v=office.14).aspx

Summary

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.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend