November 2008 - Posts

So you think GUI coolness nowadays is only related to fancy RIA stuff like Silverlight? Think again! The video above is a little project I’m working on, using jQuery plugins to show a cool way to navigate to the Lists and Document Libraries of a SharePoint site. So there are no requirements on the client, everything is done using Javascript! Nifty isn’t it? I need to polish the code a little bit and I hope to be able to release it later on this week. But i’m still thinking about a cool name .. any suggestions?

The first part of this article showed you how you can enable jQuery in SharePoint 2007 sites and pages, so in this article let's assume jQuery is up and running in your SharePoint environment. The second part of this article will show you a couple of things that you can accomplish by making use of jQuery in SharePoint 2007. It's not my goal to write about how jQuery is working, the syntax etc, for those things I kindly refer to the tutorials that are already available.

Time to get started with a first sample! Let’s assume you've got a nice SharePoint List or Document Library with a bunch of columns and some data in it. SharePoint can display that data in a nice table of course, but let's try to make that table even nicer by highlighting the entire row when the user hovers over the table. The JavaScript code, using jQuery  to accomplish this goes as follows:

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

The first line creates a function that will be called when the document is completely loaded, this function is defined inline. In this function first all tr elements of all the tables that are having the class attribute set to ms-listviewtable, are selected (this class attribute value is used by SharePoint for every table that contains list data). For every tr element, the hover jQuery function is called. This hover function takes two parameters, once again two functions: one will be executed when the mouse pointer enters the hover area (in this case the tr element), the second one will be executed when the mouse leaves the hover area. When the hover area is entered, the addClass method will add the myRowHighlight CSS class to the tr element and that CSS class is removed when the hover area is left. Of course the myRowHighlight CSS needs to be declared as well:

<style type="text/css">
   .myRowHighlight {color:red; background-color:#FFCC66}
</style>

The next thing that we need to think about is how we can tell a SharePoint page (which is showing a table containing List data), to execute that script and declare the CSS class. Probably the easiest thing to do is to make use of the Content Editor Web Part. Navigate to for example a Task list (e.g. http://yoursite/Lists/Tasks/AllItems.aspx). Next click Site Actions, Edit Page to switch the page into edit mode. Then click on the Add a Web Part button of the web part zone of that page and add an instance of the Content Editor Web Part. Click on the link open the tool pane and click on the button Source Editor.

Finally copy and paste the script and CSS class discussed above (note that script tags have been added around the JavaScript code!):

<style type="text/css">
   .myRowHighlight {color:red; background-color:#FFCC66}
</style>

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

 

Now you can click Exit Edit Mode in the top right of the screen to switch back to the normal view. When you hover over one of the rows of the table, the entire row will be highlighted.

Although adding the script and style in a Content Editor Web Part is very easy, you probably don’t want to do this in every list view for ever single List or Document Library if you want to activate the row highlighting everywhere. A better approach is to wrap the adding of the script and style in a Feature. And once again this can be accomplished by making use of the Delegate control (for more information about this approach, check out the previous part of this series).

The feature.xml simply points to a manifest which will define the delegate. The only special thing in the feature.xml is the ActivationDepenencies element: there’s an activation dependency defined that points to the feature ID of the SmartTools.jQuery feature. So the TableRowHighlight feature can only be activated if the jQuery library is loaded.

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="{7D6840E4-E31A-4c75-B2AC-71A262D0B375}"
         Title="SmartTools.TableRowHighlight v1.0"
         Description="This feature enables the highlighting of rows in List or Document Library views, when the mouse is hovered above them."
         Scope="Web"
         >

  <ElementManifests>
    <ElementManifest Location="manifest.xml"/>
  </ElementManifests>

  <ActivationDependencies>
    <ActivationDependency FeatureId="{45B66929-08B5-4715-8511-F9FB94F614EE}"/>
  </ActivationDependencies>
</Feature>

The manifest.xml is even easier, the only thing to pay attention to is the fact that this delegate control (which loads the SmartTools.TableRowHighlight.ascx user control), has a sequence that’s higher than the sequence of the SmartTools.jQuery delegate (which adds the jQuery library). So first the jQuery library is loaded, then the custom script is added.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control
    Id="AdditionalPageHead"
    ControlSrc="~/_controltemplates/SmartTools.TableRowHighlight.ascx"
    Sequence="100"/>
</Elements>

The SmartTools.TableRowHighlight.ascx user control, will simply define SmartToolsRowHighlight style, and load a Javascript file (tablerowhighlight.js). This ASCX file should be deployed to the CONTROLTEMPLATES folder in the 12\TEMPLATE folder.

<%@ Control Language="VB" ClassName="SmarToolsTableRowHighLight" %>
<style type="text/css">
   .SmartToolsRowHighlight {color:red; background-color:#FFCC66}
</style>

<script type="text/javascript" src="http://weblogs.asp.net/_layouts/SmartTools.TableRowHighlight/tablerowhighlight.js"></script>

And finally the tablerowhighlight.js javascript file will execute the function to accomplish the hover effect. This javascript file should be deployed to the LAYOUTS folder of the 12 hive.

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

What’s the result? Well, when the feature is activated on a site, the delegate is adding the ASCX to the HEAD element of the page’s HTML. This ASCX will take care of defining the style and loading the script. All the grids showing data coming from SharePoint Lists or Document Libraries, will now have the fancy hover effect. To illustrate all of this, I’ve packaged the row highlighting functionality in a nice SharePoint solution (including a nice installation wizard) which can be download from the SmartTools project on CodePlex (direct link to the releases).

In the first part of this article I'll talk about how you can enable the jQuery JavaScript library in SharePoint 2007 sites and pages. The second part of this article will focus on using jQuery in SharePoint 2007 sites and pages.

When I was at PDC’08, I attended a session about the jQuery JavaScript library (watch online). A few weeks before that Scott Guthrie announced that Microsoft would support, and even ship jQuery together with Visual Studio, along with Microsoft’s own AJAX implementation: ASP.NET AJAX. If you’ve never heard about jQuery, I defenitly recommend you to to check it out, there are some great tutorials available. The defenition of jQuery reads: "jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript." In my opinion jQuery is great because it simplifies a lot the JavaScript that you have to write if you’d like to do fancy AJAX stuff, selecting HTML elements for example is a breeze. Secondly jQuery has a big community that develops plugins for various scenarios. I already wrote and talked quite a bit about integrating ASP.NET AJAX with SharePoint 2007, so let’s check out how you can integrate jQuery as well!

First things first: you need to get the jQuery library from the official website. It comes in three varieties: uncompressed (with debug information, use it while you develop), packed (smaller, use it for production) and minified (needs to be uncompressed at the client, so slower but even smaller). The jQuery library is just a JavaScript (JS) file, so it can be loaded from any web page (html, aspx, etc). If you’d like to have IntelliSense when writing code in Visual Studio 2008 for jQuery (highly recommended of course), you need to download a Visual Studio hotfix. Now you’re good to go to make use of jQuery in your development environment.

Before I show you some things you can accomplish with jQuery in your SharePoint sites, let’s think about how we can make the jQuery library available on the ASPX pages of our SharePoint sites. There are two things that need to be done: first the JS file (the library itself) should be deployed to a location which can be accessed by SharePoint pages, secondly the library should be loaded by the SharePoint pages.

Deploying the jQuery JS file is quite easy: I recommend deploying it to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS folder on every Front End Web Server of your SharePoint Server Farm. By doing so, the file can be loaded by making use of the URL http://yoursite/_layouts/ jquery-1.2.6.min.js or http://yoursite/subsite/_layouts/ jquery-1.2.6.min.js, since the _layouts part of the URL always points to the LAYOUTS folder in the 12-hive.

Making sure that SharePoint pages will load the library can be accomplished in a couple of ways:

1) Load the library in the page you want to use it
This can be done very easily by adding for example a Content Editor Web Part to the page (or modifying the page with an editor), containing the following HTML:

<script type="text/javascript" src="http://weblogs.asp.net/_layouts/jquery-1.2.6.min.js"></script>

2) Add the script to the master page
You can add the same script tag to the master page that is used by your SharePoint sites as well, typically in the HEAD tag:

<HEAD runat="server">
    . . .
    <script type="text/javascript" src="http://weblogs.asp.net/_layouts/jquery-1.2.6.min.js"></script>
    . . .
</HEAD>

This may look very easy, but remember modifying out-of-the-box files in the 12 hive is typically not supported. So if your sites use the default master page, this is a no-go. Additionally it could be that you’ve to multiple master pages (e.g. system and site master pages) which complicate the situation.

3) Use the AdditionalPageHead Delegate Control (my favorite!)
By providing the contents for the AdditionalPageHead Delegate Control that is used by all the out-of-the-box master pages, you can make sure the the jQuery library is loaded by all the SharePoint pages. The AdditionalPageHead Delegate Control allows multiple controls to provide contents, so it’s a great extensibility scenario. To accomplish this you need to build a web user control (ASCX file), that contains the script tag to load the jQuery library:

<%@ Control Language="VB" ClassName="jQueryControl" %>
<script type="text/javascript" src="http://weblogs.asp.net/_layouts//jquery-1.2.6.min.js"></script>

There is no code-behind file required. This control needs to be deployed to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES folder on the hard drive of every SharePoint Front End Web Server. Additionally you need to have a feature that will add the control to the AdditionalPageHead Delegate Control, the feature’s manifest will look like this (assuming the control is named jQueryControl.ascx):

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control
    Id="AdditionalPageHead"
    ControlSrc="~/_controltemplates/jQueryControl ascx />
</Elements>

The feature can be scoped to any level, when it’s activated on a certain level, all the pages will automatically have the script tag in their HEAD tags. Pretty cool, isn’t it?

You can do all of this manually, but it’s of course much nicer to package the jQuery library JS file, the user control and the corresponding feature into a SharePoint Solution (WSP). I’ve created a sample Solution file, that contains a feature scoped to the Web level so the jQuery library can easily be deployed and enabled or disabled per SharePoint site. There is also an installer available that guides you through the installation process by making use of a nice wizard. You can find the solution, installer and sources as a part of the SmartTools project on CodePlex (direct link to the releases).

Ok, now we are ready to make use of jQuery in our SharePoint sites, in the second part of this article I'll show you some cool stuff you can do by integrating jQuery with SharePoint 2007.

Technorati Tags: ,,,,,,

More Posts