Skip to main content

How to use Jquery in Sharepoint 2010

To use jQuery with SharePoint, there are two things that need to be done: The JavaScript library should first be deployed to a location which can be accessed by SharePoint pages, and the library should be loaded by the SharePoint pages.
The recommended location for deployment of the JavaScript library is C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS folder. Doing so will allow the file to be loaded via the URL http://pravaham123/_layouts/jquery.js, because the _layouts part of the URL always points to the LAYOUTS folder.
There are a couple of ways you can do this.

1) Using the Content Editor Web Part to load your jQuery Library

The simplest way to load the library is by adding a Content Editor Web Part to the page and adding the following script to its Source Editor in the tool pane:
<script type="text/javascript" src="http://pravaham123/_layouts/jquery-1.5.2.min.js"></script>
You may ask, "What if I want to use jQuery for a bunch of pages, do I have to add the Web Part to each page?" The answer to that question is "Unfortunately, yes," so let's take a look at the next strategy.

2) Using the Master Page to load the jQuery Library

By using the master page to load the library, you don't have to add the Content Editor Web Part to each page on which you want to use jQuery. All you need to do is add the same script tag to your master, and in the HEAD tag:
<HEAD runat="server">
. . .
<script type="text/javascript" src="http://pravaham123/_layouts/jquery-1.5.2.min.js"></script>
. . .
</HEAD>
This would seem to be the perfect way to load the file, but be advised that when you have multiple master pages, the situation will become more complicated...

3) Using SharePoint Delegate Control

By providing the contents to the Delegate Control, which can be used by multiple master pages, you can make sure the library is loaded by all the SharePoint pages. To do this, you need to create a Web user control that contains the script tag to load the jQuery library:
<%@ Control Language="C#" ClassName="jQueryControl" %>
<script type="text/javascript" src="http://pravaham123/_layouts/jquery-1.5.2.min.js"></script>
This Web user control needs to be deployed to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES folder. Additionally, you need to have a feature that will add the control to the Delegate Control.  For an example of what the feature's manifest will look like, here is the user control named jQueryControl.ascx:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control
Id="DelegateControl"
ControlSrc="~/_controltemplates/jQueryControl.ascx />
</Elements>
The feature can be scoped to any level, and when it's activated on a certain level, all the pages will automatically have the script tag in their HEAD tags.
Now, let's start using jQuery with Sharepoint with a few examples as shown below: 

Example 1: Assume that you have a SharePoint Task List, Calendar List, or Document Library which is displayed on a nice table with rows and columns, but one day you decide  it's not so nice anymore, and try to add some animations to make it more interesting. Let's try the code sample below

$(document).ready(function()
{
$("table[class='ms-listviewtable'] tr").hover
(
function() {
$(this).addClass("myRowHighlight");
},
function() {
$(this).removeClass("myRowHighlight");
}
);
}
);

The code tries to highlight the entire row when the user hovers over the table. Of course, you need to declare the myRowHightLight CSS as well.
<style type="text/css"> 
.myRowHighlight {color:red; background-color:#FFCC66} 
</style>
Now, Add the Content Editor Web Part into you page, open its tool pane and click on Source Editor.


Next, copy and paste the script and CSS class above to the Text Entry - Web Page Dialog .





Open your SharePoint List, hover over one of the rows of the table, and the entire row is highlighted.


Example 2: Imagine you had a Publishing Page with a large chunk of content in the body. You definitely don't want your readers to scroll over the vertical bar to read entire your content. So how can you split the content into pages without any work from server side?




I would recommend that you use jQuery to page your content. Of course, you can use your own preferred method to achieve this goal, but for now, let's add the code below to the Source Editor in the tool pane of the Content Editor Web Part.

<script>
   1:  
   2: var paging = new Imtech.Pager();
   3: $(document).ready(function()
   4: {
   5:    paging.pagingContainer = $("#content");
   6:    paging.paragrahs = $("p", paging.pagingContainer);
   7:    paging.showPage(1)
   8: ;});
</script>

Now you can see your page is 
ENJOY!

Comments

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...