- Business logic
- governance
- application development
- BizTalk
- SharePoint
- database
- jQuery
- SOA
- programming
- Visual C#
- Visual Studio
- Exchange
- documents
- PHP
- services
- Microsoft Office
- customization
- Exchange server
- security
- collaboration
- .NET
- SharePoint 2010
- CA
- CodePlex
- developer
- search
- document management
- portal
- WSS
- Web development
- Web sites
- authentication
- XML
- Microsoft
- software
- policy
- MOSS
- Web Parts
- Office
- Silverlight
- tools
- sandbox
- SharePoint Service Account
- InfoPath
- ASP.NET
- Windows
- server
- architecture
- Libraries
- SharePoint administration
jQuery and SharePoint Part I - The Easy Way to Load the jQuery Library
jQuery and SharePoint Part I - The Easy Way to Load the jQuery Library In this article, we are going to show how to load the jQuery library as a feature in SharePoint 2010. This way will also work for SharePoint 2007. Just for good measure, we are going to also include the excellent SPServices library and some custom CSS files in our feature. I want to include/acknowledge the folks and tools that made this article possible. Please click through and learn more about these great resources.- Jan Tielens. The feature described in this article is merely a logical extension of a solution presented by Jan in this blog post: http://weblogs.asp.net/jan/archive/2008/11/20/sharepoint-2007-and-jquery-1.aspx
- jQuery http://www.jquery.com
- SPServices Project http://spservices.codeplex.com
- SharePointBriefing. Here is a link to an earlier article of mine that speaks about some of the basics of using jQuery, SPServices and the SP.UI namespace. You can combine that information with the information herein and get started using jQuery in SharePoint right away. http://www.sharepointbriefing.com/spcode/article.php/3900961/Working-with-jQuery-and-the-SPUI-Namespace-on-SharePoint-2010.htm
Components
Our solution is going to incorporate the following components:
- JavaScript and CSS Injection Feature - This is a SharePoint feature, scoped at the Site Collection level, that will be installed on the SharePoint farm and activated for individual site collections.
- Control Template - This is where the main "work" will take place. Our control is going to inject the jQuery library, the SPServices library, and a custom CSS file into every page for a SharePoint Site Collection.
- Custom Layouts Folder - In order to ensure that our information is protected from any future SharePoint updates, we are going to create a folder in the SharePoint LAYOUTS directory. It will therefore be accessible to all SharePoint sites on the farm.
- JavaScript Libraries and Custom CSS File - These will be maintained in our custom layouts folder. In order to allow for further segmentation for ease of use, we are going to include a couple of additional folder for the JavaScript libraries and our custom styles. Our folder structure will look something like this:

Figure 1
Setting Everything Up
Now that we are familiar with the structure of our solution, let's start setting things up. Follow the steps below and you will be ready to install your feature in no time.
- Create the folder that will house your feature's XML files. In the example above, it is called "
ClientABC_JS_Injection" - Create a blank text file named "
Feature.xml" in the folder you created in Step 1 - Insert the following code into "
Feature.xml"<?xml version="1.0" encoding="utf-8" ?> <Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="{45B66929-08B5-4715-8511-F9FB94F614EE}" Title="ClientABC JavaScript and CSS Injection" Description="This feature loads, or injects, the jQuery JavaScript library (version 1.4.4), as well as other jQuery add-ons, and custom CSS, in every web page of this site." Scope="Site" > <ElementManifests> <ElementManifest Location="Manifest.xml"/> </ElementManifests> </Feature> - Create a blank text file named "
Manifest.xml" in the folder you created in Step 1 - Insert the following code into "
Manifest.xml"<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Control Id="AdditionalPageHead" ControlSrc="~/_controltemplates/JavaScript_CSS.Injection.ascx" Sequence="10"/> </Elements> - Create the appropriate folder(s) underneath the LAYOUTS folder
- Add the jQuery library, the SPService library, any custom JavaScript file(s) and any custom CSS files in the appropriate folder under LAYOUTS
- NOTE 1: It is a very good idea to work with the minified jQuery and SPServices files
- Note 2: The custom JavaScript file is sometimes a good location for your
$(document).ready function. If you don't know what this is, and you are new to JS/jQuery, don't worry you will learn.
- Create your custom control file under the
CONTROLTEMPLATESfolder - Using your favorite editor, insert the following code into the custom control
<%@ Control Language="C#" ClassName="ClientABCjQuery" %>
NOTE: It is also a good idea to inject the
SP.UInamespace code to allow for easy use of modal dialog windows in SharePoint. You can learn more about theSP.UInamespace here: http://msdn.microsoft.com/en-us/library/ee552096.aspx. Some sample code for injecting this modal dialog functionality, in its simplest form, might look like this:<script type="text/javascript"> //Handle the DialogCallback function DialogCallback(dialogRequest, returnValue) { } //Open the Dialog function OpenModalDialog(dlgUrl, dlgTitle) { var options = { url: dlgUrl, title: dlgTitle, autosize: true, dialogReturnValueCallback: DialogCallback }; SP.UI.ModalDialog.showModalDialog(options); } </script>- You are done. You have created all of the artifacts you need for your feature.
Place that directly under the earlier code for the custom control
Installing the Feature
In order to install the feature, run the following PowerShell command.
Install-SPFeature ClientABC_JS_Injection - (This is simple the name of your folder under the FEATURES directory.)
Now, all you have to do is activate the feature for your site collection. You can do this through the SharePoint UI (Site Actions, Site Settings, Site Collection Features), or you can also accomplish this through PowerShell, by using:
Enable-SPFeature ClientABC_JS_Injection -Url <<URL to Site Collection>>
That's it. You should now have all of the files listed in your custom control automatically injected into the <HEAD> tag of every page on your Site Collection.
Making Changes
Here is the best part of this approach - once you have installed and activated your feature, adding additional JS libraries or CSS files is as simple as dropping them into the appropriate folder under the LAYOUTS directory, and adding a line to your custom control.
Please stay tuned to this space. In our next installment, we are going to show some tricks for using jQuery for rich user experiences in SharePoint.

Discover how to start developing for the Android platform with this extensive guide, which provides a reference to the Android platform as well as a look at developing your first Android application. You'll explore the top 10 features for developers as well as learn design and development tips that go beyond the phone and target tablet development as well.