Feeds:
Posts
Comments

Archive for the ‘jQuery’ Category

Adding a box or rounded corners around a SharePoint web part can be challenging due to the way that SharePoint renders it’s web parts in a <table> layout. This article explains how to add extra <div> elements around the SharePoint web parts. Then by using the ‘sliding doors’ css technique we can add a flexible box arround the SharePoint web part and even include rounded corners.
This tutorial already assumes that you know what SharePoint master pages are and how to work with css and JavaScript files.

Problem.

The HTML code of a SharePoint web part is made out of tables. Depending on the web part options you selected extra  tables and css classes can be added, removed or nested.

The basic HTML markup of a SharePoint web part looks like this

    <table cellspacing="0" cellpadding="0" border="0" width="100%" class="s4-wpTopTable">
        <tbody>
          <tr>
            <td>
            <table cellspacing="0" cellpadding="0" border="0" width="100%">
                <tbody>
                    <tr class="ms-WPHeader">
                    <!-- web part title and menu goes here -->
                   </tr>
                </tbody>
            </table>
            </td>
          </tr>
          <tr>
            <!-- actual web part content goes here -->
          </tr>
        </tbody>
    </table>

This table layout does not leave much room to add a flexible box layout.

The solution.

By using jQuery we can add extra <div> elements to each web part on the SharePoint page. We will use these extra <div> elements  to create a box arround the web part.

For simplicity sake I have put all the needed files together in a folder “boxdemo”. I simply copied these files to the ‘_layouts\styles\’ folder of my SharePoint hive.

Note: putting your files in a folder on the SharePoint server harddrive is not best practice! But for tutorial purposes this will do :-) .  Always use a SharePoint feature when deploying files to a SharePoint server.

The actual box around the web part will be created by simple images. You can use this website to generate the desired images.

For this solution we will need 6 images

  1. l.png   (left image)
  2. r.png (right image)
  3. t.png (top image)
  4. b.png (bottom image)
  5. tl.png   (top left image)
  6. bl.png  (bottom left image)

And also 2 extra files. A stylesheet and a javascript file. you can already create these and leave them empty for now.

  1. style.css
  2. boxing.js

We will add code to these empty files later on.

Adjust your master page

In order to make the boxing work we need to add 3 links to our master page. These links points to the jQuery library, our styling file and our JavaScript file.

Open your master page and add the following links to the <HEAD> section of your master page:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script src="/_layouts/Styles/boxdemo/boxing.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="/_layouts/Styles/boxdemo/style.css" media="screen" />

I use the jQuery hosted at googleapis.com. In corporate environments this domain is sometimes blocked. If this is the case you should download the jQuery library and put it in the _layouts\styles folder together with the other files.

Javascript

To dynamically add the extra <div> elements arround your webparts we will need a piece of javascript.

Open the (empty) boxing.js file you created earlier and add the following code

/* Add extra div arround webparts so they can be styled with a box. */
$(document).ready(
    function RoundWebPartHeader() {
        $(".s4-wpTopTable").each(function () {
            $(this).addClass("wp-head-round")
            .wrap('<div class=\"box-wp-dialog\">')
            .wrap('<div class=\"box-wp-content\">')
            .wrap(' <div class=\"innerbox\">')
            .parents("div:first").parents("div:first").parents("div:first").prepend('<div class=\"top\"><span>&nbsp;</span></div>')
            .append('<div class=\"bottom\"><span>&nbsp;</span></div>')
        });
    }
);

This JavaScript uses jQuery to find all HTML elements with the css class “s4-wpTopTable”.  By default this css class is assigned  to every web part on the page by SharePoint.

For each HTML element with css class “s4-wpTopTable” the code does the following :

  • adds a css class “wp-head-round”
  • wraps the web part in a <div> with class “box-wp-dialog”
  • wraps the web part again with a <div> with class “box-wp-content”
  • inside the <div> with class “box-wp-dialog”, a new <div class”top”> is added
  • inside the <div> with class “box-wp-dialog”, a new <div class”bottom”> is added

When this is done the SharePoint web part will be encapsulated by various <div> elements.
And the HTML will look like this

<div class="box-wp-dialog">
  <div class="top">
     <span>&nbsp;</span>
  </div>
  <div class="box-wp-content">
    <div class="innerbox">
      <! -- The webpart table HTML is included here -->
      <table cellspacing="0" cellpadding="0" border="0" width="100%" class="s4-wpTopTable">
      </table>
    </div>
  </div>
  <div class="bottom">
    <span>&nbsp;</span>
  </div>
</div>

Go ahead and try it out. Visually there will be no changes (yet). But the extra <div> should be there in the HTML. Keep in mind that the extra HTML elements  are added dynamically on the client side. You will not see these new HTML elements in the source of the page. You need to inspect the HTML with FireBug or with the “Internet Explorer Developer Tools” (F12)

Styling

So now that you have the (extra) <div> elements in our HTML, we can focus on actually styling and creating the box. The last step in our tutorial.

Open the (empty) style.css file and add the following css code.

.box-wp-dialog .top
{
  background-image:url(/_layouts/Styles/boxdemo/tl.png);
  background-repeat: no-repeat;
  background-position: left top;
  height: 12px;padding: 0px 0px 0px 10px;
}
.box-wp-dialog .top span
{
  background-image:url(/_layouts/Styles/boxdemo/t.png);
  background-repeat: no-repeat;
  background-position: right top;
  width: 100%;height: 12px;float:  left;
}

.box-wp-dialog .bottom
{
  background:url(/_layouts/Styles/boxdemo/bl.png) no-repeat left bottom;
  height: 10px;
  padding-left:15px;
}

.box-wp-dialog .bottom span
{
  background:url(/_layouts/Styles/boxdemo/b.png) no-repeat right bottom;
  height: 10px;
  display:block;
}

.box-wp-dialog  .box-wp-content
{
  background: url(/_layouts/Styles/boxdemo/l.png) repeat-y top left;
  padding-left: 2px;
}

.box-wp-dialog  .box-wp-content .innerbox
{
  background: url(/_layouts/Styles/boxdemo/r.png) repeat-y top right;
  background-color: #FFFFFF;
  padding: 5px 5px 0px 10px;
}

This CSS code simply defines the correct background images on our web part to create the illusion of a box.  Save the file and reload your page. Make sure your browser is not using a cached copy.

You should see something similar to this

 

You can change the images to the desired color. Play around with the height, padding, etc…

Happy coding…

Read Full Post »

I recently ran into a problem where my web part, that uses jQuery, didn’t work anymore on the SharePoint production server. After some investigation it became clear that it was caused by another  SharePoint feature that was enabled on the site.

This SharePoint feature used it’s own JavaScript framework that conflicted with jQuery. jQuery uses some keywords and expressions in a fashion that does not match with other libraries. jQuery Has foreseen this and has functionality in place to resolve this.  More information on how to solve this can be found here.
After applying these changes the two frameworks were working happily together.

For more detailed information about the issue I encountered see here.

Hope it helps.

Read Full Post »

Update 2009-04-23 : Thanks to Coveo support. I was able so resolve this issue. See below for details.

While I was creating a custom web part using the jQuery treeview plugin,  I ran into an issue with Coveo Search.  The click events ( to expand / collapse the treeview nodes) didn’t work anymore. The web part works smoothly on my development and test machine but those machines do not have Coveo Search installed.

After debugging, and manually removing redundant JavaScript files , the CommonClientScripts.js seems to be the reason. This file is ‘injected’ by Coveo at the beginning of the page’s <body> tag.  This injection is done by calling the following url:
{your SharePoint url}/_layouts/Coveo/SharePointCustomContent.aspx?k=ccs&z=1006065576

The injected JavaScript file does a lot of event handling and subscribes to various events.  My guess is that registering to all these events breaks the jQuery functionality.

Disabling the Coveo Search features at site collection level solves the problem but (of course) also disables the search functionality in the site collection.

I haven’t found a solution yet. If you have some tips feel free to leave them in the comments.

[update 2009-04-23]
Thanks to Coveo support I was able to solve this issue. As they already stated in the comments it was indeed a very simple solution.

JQuery uses some keywords and expressions in a fashion that does not match with other libraries. Luckily jQuery has functionality to solve this as explained in this article

In short: You have to add an extra line (jQuery.noConflict();) and replace all your ‘$’ with jQuery.

Read Full Post »

Introduction.

Lately, I ‘ve been exploring the use of jQuery in ASP.NET applications.  If you haven’t got the chance yet to try it out, do so!! It is certainly an added value, even when programming in .NET.

However the modal forms can break the postback functionality of your ASP.NET pages. Read below for how to fix it.

What is a modal form ?

A modal form is nothing more than a hidden <div> element that is set to visible by running a small piece of JavaScript code in the  browser. It feels very responsive and fast since there is no round trip to the  server. The <div> element containing the modal form is already inplace the first time you load the page.  The JavaScript just toggles the <div> element between visible and invisible.  If you know a little bit about JavaScript and CSS is very easy to understand. 
See some examples here.

However when you have ‘postback’ functionality in this <div> element the modal form breaks. The code behind of your ( for example) button clicks ( asp:Button that is) never execute.

Sample application in ASP.NET

Preparing your Visual Studio environment.

  1. Go to the jQuery download page and download the latest version
  2. Go to the SimpleModal webpage and download the latest version of the jQuery plugin
  3. Start Visual Studio and create a new website project.
  4. Create a new folder (I called mine: ‘scripts’)
  5. Drag and drop the jQuery JavaScript file into the scripts folder.
  6. The modal plugin contains various folders. I created a new folder called ‘modal’ in my scripts folder and I copy pasted all the modal files in this new folder

You now have everything in place. You solution explorer should look like this :

Open the default.aspx in HTML view and add a link to the 2 JavaScript files in the <head> section of your HTML page (default.aspx ). Also add a link to the SimpleModal style sheet  (also included in the SimpleModal download).

screenhunter_05-mar-18-21051

Next step is to add the HTML elements. I created the following elements :

  1. A link that opens the modal dialog encapsulated in a <div> element
  2. A label ( This one will be changed by the postback )
  3. A hidden div element. This is our modal form.

So your results looks like this:
HTML elements
Note: the ID attributes ‘Actionlist’ and ‘ModalWithButtonContext’ above. These ID’s identify the elements for jQuery.

You now have all the elements in place. We only need to add the jQuery functionality. Go back to the <head> element of the page and insert the following small JavaScript. Note that it references to the id attributes of the divs, so make sure they match !!

screenhunter_06-mar-18-2106

Now we add some logic to the button. This logic will run when we click on the button in our modal form. For simplicity sake we will only update the Label1 control on the main page with the current time. You can add of course your own code instead. Open the code behind file and add the following code.

code behind

Save all your files and try running the application. As you will notice the postback logic does not execute. The label is not updated. Seems like the postback code is broken.

The fix

The problem is that the jQuery SimpelModal code adds the modal form to the end of the <body> tag. ASP.NET however, needs all of the controls within the <form> tag in order to execute properly. So our modal form is ‘out-of-scope’ and will not trigger the postback events.
Luckely we can modify this in the JavaScript code. It sort of a hack but it works for me.

  1. In Visual Studio go to the solution explorer and open the jquery.simplemodal.js file.
  2. Find and replace all occurrences of appendTo(‘body’) with appendTo(‘form’).
  3. Save the file and run your code again.

When you click now, on the button in the modal form. The postback will trigger, adjust the text in the label and close the modal form.

Result

Feedback and comments welcome.

Read Full Post »

Follow

Get every new post delivered to your Inbox.