1,804 articles and 14,937 comments as of Thursday, May 19th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Tuesday, June 16, 2009

Quick and Easy: Create Your Own jQuery Sandbox for SharePoint

This is another post in Paul Galvin’s on-going series on how to use jQuery with SharePoint. If you want to learn more about jQuery, I highly recommend: jQuery in Action by Bear Bibeault and Yehuda Katz.

Getting started with jQuery in SharePoint is surprisingly easy (to me).  (I do have serious questions about a “best practices” approach to deploying these things to production, but that’s for another day).  I’ve just started playing with this technology and to that end, I created a sandbox environment to use.  If you’re looking to get started with jQuery, you may find this approach useful.

1. Create a Blank Site

Create a blank site somewhere in your site and call it something clever like “jQuery Sandbox”.

2. Download jQuery

You can download the jQuery javascript library from here:

Save that to to your desktop.

I have been using the “minified” version.

3. Create a SharePoint Document Library

In your sandbox site, create a document library. 

4. Upload the jQuery Library to SharePoint

Access the doc library you just created and upload the jQuery library.

5. Create a Custom SharePoint List

I’ve started with a custom list because I want to muck about with standard SharePoint forms.  You could also create a page in a pages library or web part pages and probably a lot of other places. 

Add some columns to the custom list so that you have something to run jQuery against.  My initial objectives were to:

  1. Hide a field.
  2. Assign a value to a field.

With that objective in mind, I added two text fields.  Over time, I’ll be playing with links, images, lookups, etc. 

6. Modify the NewForm.aspx Web Part Page and Add a Content Editor Web Part

This is a little black magic-ish , in that it’s a new concept to me.  I first learned about this from Paul Grenier, SharePoint jQuery Superstar, at his CodePlex project site: http://spff.codeplex.com/

Follow these steps to add a CEWP to the same page that shows NewForm.aspx for any custom list:

  1. Access the custom list and click New.
  2. Append the following to the URL: PageView=Shared&ToolPaneView=2

That will transform your boring vanilla data entry form from something like this:

Quick and Easy Sandbox

To this:

Quick and Easy Sandbox

Add the content editor web part to the page.

7. Write Your First jQuery Code

Open up that CEWP in the code view and add the following:

Quick and Easy Sandbox

Here’s the actual code if you want to copy/paste:

<script type="text/javascript">
      src="../../jQuery%20Library/jquery-1.3.2.min.js"
      </script>
      <script type="text/javascript">
      $(function() {
      $('#resultsID').html('There    are ' + $('a').size() + ' a tags tags on this page.');
	  });
      </script>

Result:

      <div id='resultsID'></div>
      /result

Note that the first <script> tag is referencing the actual jQuery library.  Presumably, these things change over time, so you’ll want to make sure you a) use the right name and b) point it to the correct SharePoint document library.

Bask in the Glory

If you did it correctly, you’ll see a result similar to the following:

Quick and Easy Sandbox

Wrapping Up

This isn’t the only way to get started, but it’s quick, easy and isolated from your existing SharePoint environment. 

Paul Galvin, MVPPaul Galvin, Microsoft MVP – SharePoint
Web site: Paul Galvin’s SharePoint Space

Paul is a Solutions Architect currently working most closely with Microsoft Office SharePoint Server 2007. He was recently awarded Microsoft MVP – SharePoint status for his work with the SharePoint community.

View all entries in this series: PaulGalvin - Quick and Easy jQuery»
 

Please Join the Discussion

4 Responses to “Quick and Easy: Create Your Own jQuery Sandbox for SharePoint”
  1. Eric J says:

    The copy-and-paste text has an extraneous right-angle-bracket at the end of the first line.

  2. Not anymore :-) Thanks Eric.

  3. Elbio says:

    I wasn’t able to copy/paste the code, but I entered this code into the source editor and it didn’t work for me:

    <script
    type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"

    $(function() {

    $(’#resultsID’).html(;There are ‘ + $(’a').size() + ‘ a tags tags on this page.’);

    });

    Result:

    /result


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!