jQuery and SharePoint 2010: the Basics
Setup jQuery for SharePoint
First things first, let’s make sure we’re properly setup. If you are using SharePoint behind a secure server (as part of an intranet possibly), then you’ll probably want to load up a local copy of the jQuery script instead of grabbing it from jquery.com each time the page loads. So grab the latest build from jquery.com and dump it in “Style Library/scripts/jquery.js”.Next, go to your master page and put the following script (depending on where your file is) anywhere within the HEAD section:
<script type="text/javascript" src="/Style Library/scripts/jquery.js"></script>
And that’s it! Now every time a page loads, you’re pulling in all of
jQuery’s power. Minified, the file is only about 31kb, so (considering
everything else SharePoint is loading) this isn’t that much of a burden.
But you can also just load the jQuery script in page layouts, or even
within individual pages.Now that you’ve got jQuery loaded, it might be in your best interest to create another .js file that holds all of your custom scripts. You can choose to do scripts on a per page or page layout basis as well, but this keeps things more organized. So create a “myscripts.js” file in “Style Library/scripts” (the same folder we put jQuery.js in). And don’t forget to reference it in your master page, right under the jquery code. So now the above code is amended to read:
<script type="text/javascript" src="/Style Library/scripts/jquery.js"></script>
<script type="text/javascript" src="/Style Library/scripts/myscripts.js"></script>
Now that we’ve got everything loaded, what can we do?What can you do with jQuery in SharePoint?
Here are just a few things:- Markup your body content and make it interactive
- Make an “instant” search/filter box
- Create a slideshow of content from a picture library
- Create tabs (without jQuery UI)
- Use SPServices to really hook into SharePoint with jQuery
Markup your body content and make it interactive
I use this for something like an FAQ section. We’ll put every question in an H4 tag with a class of “question” and every answer in a span with a class of “answer” directly after the h4. That’s all we have to do to our HTML source in the page. Then in our custom scripts file (above) we add the following jQuery code:$(document).ready(function(){
$("span.answer").slideUp(); // hides all of the answers
$("h4.question").click(function(){ // when you click an h4
$(this).next("span.answer").slideToggle('fast') //the answer slides down
})
});
Keep in mind you may want to add some CSS that make the h4 seem more
like a link (cursor:pointer, etc.) so people know they should click.Make an “instant” search/filter box
Check out this post about how to create this. You can create a filter, and then append it to any list across your site.
Create a slideshow of content from a picture library
I’ve written a separate post with precise instructions about how to do this, but here’s the general idea:
- Create a picture library
- Make a new page
- Put a CQWP in the page, and wrap it in some divs with specific classes
- Use the tinycarousel plugin to convert that Content Query WebPart into a slideshow/carousel
- Style with CSS
Create tabs (not the jQuery UI kind)
Check out this post on creating tabs using jQuery, but without using the jQuery UI. It’s a bit easier with the UI plugin, but again, the idea is:- Create the tabs in HTML
- Create each panel in HTML
- Add the jQuery
- Style it with CSS
Use SPServices to really hook into SharePoint with jQuery
I’ve started writing what is turning into a small series on using jQuery with SPServices. Some of the things you can do:Rollup items from multiple lists
Rollup from multiple lists and put the results in a new list
Rollup articles across a site collection
No comments:
Post a Comment
Note: only a member of this blog may post a comment.