Add-in Express Blogs
spacer

Office 365 – Using jQuery with SharePoint Online

Posted on Monday, January 30th, 2012 at 12:33 pm by Pieter van der Westhuizen

JavaScript, whether you love it or hate it, it’s here to stay. Now, JavaScript does have its quirks and irritations, especially if you’re used to managed code such as VB.Net or C#. However, despite the bad rep a lot of folks have given it, it is one heck of a powerful language.

If you’re like me, you like to get the job done. Sure, sure, it’s fun to dig into the code to see exactly how stuff is done, but that is not really an option when you have a deadline. That is why I choose to use jQuery when I need to liven up a web page or do some web programming magic.

JQuery was first released in 2006 and has since become one of the most popular JavaScript libraries on the web. Microsoft has thrown its weight behind it and now also provides developer support as well as adopting it in their AJAX and ASP.Net MVC frameworks.

So, how do we use all this jQuery goodness in SharePoint Online? That is what I hope to show you in today’s article where we’ll write a fairly simple SharePoint WebPart and add some jQuery logic to it.

Referencing jQuery from SharePoint

Before we can use jQuery in our SharePoint web part we need to find a way to reference the library. There are a few options available to us, of which one is to simply add a reference to the script via a Content Delivery Network (CDN). In the example below we’re referencing the jQuery library via Google’s CDN:


<script src="/img/spacer.gif"> 

Using a content delivery network like Google’s is a great way to speed up your website as the chances are the user visiting your website has already visited a website that also references a library in the Google CDN and it is already in the users’ cache. To see a list of libraries available via the Google CDN, have a look at the Google Libraries API – Developer’s Guide.

Another option and the one we’ll be using for this example is to create a SharePoint document list to hold all your scripts. Create a new document library in SharePoint by selecting New Document Library from the Site Actions menu.

spacer

Name the library Scripts and click Create.

spacer

Next, we need to add the jQuery library file to the Scripts SharePoint library. Head over to jquery.com/ to download jQuery. Name the file jquery.js and upload it to the Scripts library. One benefit of storing your javascripts in a SharePoint list is that you can reuse it.

The nice thing about jQuery is that it has a whole host of community developed plug-ins, one of which is Hovercard. We’ll use this plugin to show more details about items in a SharePoint list called Books. Download Hovercard and also add it to the Scripts SharePoint library.

Creating the SharePoint WebPart

With the jQuery library and Hovercard plugin file uploaded, let’s create a new Empty SharePoint Project in Visual Studio 2010.

spacer

Select a local SharePoint site for debugging and make sure to select Deploy as a sandboxed solution.

spacer

Next, add a Visual Web Part (Sandboxed) item to your project. If you don’t see the Web Part (Sandboxed) item, you need to install the Visual Studio 2010 SharePoint Power Tools.

spacer

We won’t add any visual controls to our web part, but we’ll do everything in mark-up. The entire code listing for the Visual Web Part is as follows:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="jQueryWebPart.ascx.cs"
    Inherits="jQueryAndSP.jQueryWebPart.jQueryWebPart" %>
<script src="/img/spacer.gif"> 

In the code above we referenced the jQuery and the Hovercard files we’ve added to our Scripts document library earlier. The ExecuteOrDelayUntilScriptLoaded method is a built-in SharePoint method, which will execute the specified function only after, in this case the sp.js javascript file, is loaded. SP.js is a javascript library that gives us access to the SharePoint client object model.

The LoadList function retrieves all items from the Books SharePoint list and the ListRetrieved function loops through all the list items and appends the values, using jQuery, to the books Div.

Finally, we loop through each html element that has a class name containing the word book and add a Hovercard to it with the elements’ title attribute as the Hovercards’ detailsHTML.

Packaging and deploying to Office 365 SharePoint Online

Once everything is tested you can package the SharePoint solution by selecting Package from the Visual Studio Build menu. This will create a .wsp file in your projects’ bin folder. Check your Output window for the full path to the file if you’re not sure.

spacer

Next, log into your Office 365 SharePoint Online account, and click on the Site Actions > Site Settings menu item.

spacer

Next, click on the Solutions link under the Galleries heading.

spacer

Click on the Upload Solution button on the Solution tab:

spacer

Select and upload the .wsp file and click OK. When prompted activate your solution by clicking on the Activate button.

spacer

Navigate to a SharePoint page, and insert the jQuery web part we’ve just uploaded.

spacer

Once you’re finished editing the page, and the page loads, it will display a list of all the items in the SharePoint Books list. When you hover the mouse cursor over a book title it will display the books’ synopsis:

spacer

Thank you for reading. Until next time, keep coding!

Available downloads:

C# sample project

You may also be interested in:

  • Ribbon Designer for Microsoft SharePoint and Office 365
  • SharePoint Ribbon UI customization with Ribbon Designer for SharePoint and Office 365
  • Video: Ribbon Designer for SharePoint and Office 365
Office 365, SharePoint
Filed under Office 365 Development
Moving your existing Access database to Office 365
Office Newswire: Office 15 begins technical preview – 2.1.2012

Post a comment

Click here to cancel reply.

List of authors

  • Andrei Smolin
  • Dmitry Kostochko
  • Eric Legault
  • Eugene Astafiev
  • Eugene Starostin
  • Fedor Shihantsov
  • Fokke Post
  • Maurice Calvert
  • Pieter van der Westhuizen
  • Renat Tlebaldziyeu
  • Sally Peck
  • Sergey Grischenko
  • Ty Anderson

Have any questions? Ask us right now!

Add-in Express Feedback
If you have any questions or other feedback about the blog posts or our products, please write to us using this form.
  • Products
  • Add-ins for Office in .net
  • COM add-ins in Delphi
  • Add-ons for Internet Explorer
  • Regions for Outlook and VSTO
  • Disable Outlook security
  • Website
  • Links
  • Blogs
  • Technologies
  • Add-ins for Office in VSTO
  • Plug-ins for Outlook Express
  • Advanced regions for Outlook
  • Custom controls for Office commandbars
  • Free tools
  • VSTO Support for Delphi Prism
  • MAPI Store Accessor
  • Advanced Search for Internet Explorer
  • Support resources
  • Ribbon Designer for SharePoint & Office 365 Guide
  • Add-in Express for Office and .net Guide
  • Add-in Express for Office and VCL Guide
  • Add-in Express for Internet Explorer Guide
  • Outlook Security Manager Guide
  • Samples
  • HowTo samples for developers
  • Toys: add-ins for Excel, Word, Outlook
  • Sign In
  • |
  • Sitemap
  • Company
    • Home
    • Latest news
    • Blogs
    • Customers say
    • Success stories
    • Customers say
    • Our products in action
    • Add-in Express reviews
    • About Add-in Express
    • About us
    • Team
    • Contact us
  • Products
    • Products
    • Ribbon Designer for SharePoint and Office 365
    • Add-in Express for Office and .net
    • Add-in Express for Office and VCL
    • Regions for Outlook and VSTO
    • Add-in Express for Internet Explorer and .net
    • Security Manager for Microsoft Outlook
    • Technologies
    • Add-
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.