Saturday 17 December 2011

Add Javascript to your SharePoint 2010 site\Pages with a solution(wsp)


Last week i had a requirenment where i needed to add some javascript files that contained my javascript Client Objetc model code to our site Master Page. The files basically neededs to be loaded on each page of our SharePoint site so i though adding them to our master page is probbaly the best option.
But then there was a twist we can only do this with a wsp solution\feature and not by manually adding references to js file.

So after some research i stteletd at the im-famous Scriptlink feature. This feature once activated will add refrences to the javascript files in SharePoint site's master page.

Lets look at some high level Steps -

1. Create an Empty VS2010 project.

2. Add a new Feature (Right click feature -> Add Feature)

3. Next, Right click on the project and add a new "Empty Element" item.

4. Add the below in your elements.xml file.
//Referencing the Script
<CustomAction
Id="OwnersButton.Script"
Location="ScriptLink"
ScriptSrc ="/_layouts/MyCustomScripts/myCustomJs.js"/>
</Elements>

5. Next build and deploy your solution.

6. Once you activate the respective fetaure a refrence to the file myCustomJs.js should be added into your master page. Veriy your master page for testing.

Ads by Google

No comments:

Post a Comment