Introduction
During the course of a customization and branding effort in SharePoint you are likely to find that you want to style web parts to look different from (Dare I say better than?) the stylistic treatment default to SharePoint.
In my quest for finding a way to improve the look of web parts, I discovered Microsoft’s markup for web parts does not allow for much web part style manipulation, as there are not enough CSS hooks. Nor could I modify the HTML inside a web part zone in order to wrap web parts with <div> tags for styling purposes—you can’t edit the contents of web part zone directly. So I had to settle with wrapping <div> tags with classes around web part zones, which got me where I wanted to go somewhat, but it failed to allow me to apply treatments like background colors, borders, rounded corners, and drop shadows to individual web parts—I could only apply them to the wrapper around the web part zone.
So I wrote some jQuery to inject into the DOM <div> tags and classes for web parts that I could then use as hooks in my CSS.
On March 17, 2010, EndUserSharePoint.com published a post I wrote about wrapping web parts with jQuery that included the necessary JavaScript function, but not much else. I apologize my post did not include the other assets needed, like the CSS and images. Moreover, with that post I had taken the approach of creating a custom master page from which my function would be called (my usual approach for SharePoint customization efforts). Several commenters noted they were looking for an encapsulated solution they could drop on the page via a Content Editor Web Part rather than a solution that would require them to modify master pages or page layouts. While I advocate referencing the solution from the master page or page layout, I understand that is not possible in all situations and that EndUserSharePoint.com leans towards solutions easily implemented by content contributors and site administrators.
The following is that solution, all packaged up, ready for you to plug-and-play.
Solution
This solution will work for WSS 2007 and MOSS 2007, and it makes use of Mark Miller’s Scripting Resource Center. Please follow Mark’s instructions so you have the proper libraries to follow along below. My code includes paths to these libraries and references them by name, so it is important to have your Scripting Resource Center at the root of your site collection and that you use the filenames as given below. The script, CSS, HTML, and images for this solution will be stored in your Scripting Resource Center library, to which you will reference from a Content Editor Web Part you drop on your page. I am also supplying the pre-configured Content Editor Web Part as a .dwp that you can add to your web par gallery or import onto the page.
Step 1. Create the Scripting Resource Center libraries.
See http://www.endusersharepoint.com/2010/01/05/build-a-sharepoint-scripting-resource-center/. I assume you have created your Scripting Resource Center at the root of your site collection and assigned it the path /ScriptingResourceCenter (any deviations will require adjustments to the solution’s HTML file).
Step 2. Upload the jQuery library into the “Resources-jQuery” library of your Scripting Resource Center.
You may or may not have a jQuery library already.
- If you already have your jQuery library version of choice, upload it into your “Resources-jQuery” library.
- If you do not currently have a copy of the jQuery library, go to http://jquery.com and click the “download(jQuery);” button. Upload this file into your “Resources-jQuery” library.
Note: In the code provided, I assume you have uploaded the latest jQuery library version as of the time of this post, which is v1.4.2, and that the file is named jquery-1.4.2.min.js. If you are using a different version or file name, you will need to update the solution’s HTML file (wrapwebparts2007.html) to use the correct file name.
Step 3. Upload the JavaScript, CSS, and HTML files into the “Resources-Scripts” library of your Scripting Resource Center.
Download the following files from my Box.net folder and upload to /ScriptingResourceCenter/ResourcesScripts:
Note: The script in wrapwebparts2007.html that tests for and writes the reference to the jQuery library is borrowed from Christina Wheeler’s post “Transparent Overlays for SharePoint Interface Enhancement.”
Step 4. Upload the images into the “Resources-Images” library of your Scripting Resource Center.
Download the following images from my blog and upload to /ScriptingResourceCenter/ResourcesImages:
- standard_inner_wptitle_lightgrey_bg.gif
- wpcorners_f0f3f5_tl.png
- wpcorners_f0f3f5_tr.png
- wpcorners_ffffff_bl.png
- wpcorners_ffffff_br.png
Step 5. Upload my Content Editor Web Part into your site collection’s web part gallery and add it to your page.
Download the following web part from my Box.net folder and upload into your site collection’s web part gallery (located at /_catalogs/wp/Forms/AllItems.aspx). The web part’s name will be “jQuery Wrap Web Parts 2007.”
On the page you wish to use this script, add the “jQuery Wrap Web Parts 2007” web part. If for any reason you need to change the path to the solution’s HTML file, edit that web part and change the Content Link property. In our example, we’re using the Content Link field value “/ScriptingResourceCenter/ResourcesScripts/wrapwebparts2007.html”.
Step 6. Bask in the glory of your new web-part-modifying jQuery friend.
If you use this solution and the implementation is viewable by the public, please leave a link in the comments. I’d love to see how it’s being used.
Notes
- You may experience a delay between time the web part is written to the page and when the solution’s stylistic treatment is applied. This is due to the loading/application of the jQuery library and the solution’s script file.
- This solution will only work for WSS 2007 and MOSS 2007. I will work on a 2010 version and let you know when it’s ready.
- This solution was tested in IE 7, IE 8, and Firefox 3.6.
- Any deviations from the paths and filenames used the solution will require adjustments to either the solution’s HTML file, the web part’s Content Link path, or both.
- In the code provided, I assume you have uploaded the latest jQuery library version as of the time of this post, which is v1.4.2 and that the file is named jquery-1.4.2.min.js. If you are using a different version or file name, you will need to update the solution’s HTML file (wrapwebparts2007.html) to use the correct file name.
- If you do not want to make this web part available in the gallery, you have the option to import the web part onto the page on which you wish to use it using the standard web part import process.
- The CSS makes use of the “!important” property attribute in order to force the override of certain SharePoint 2007 default styles. While it is not necessary for every CSS property I write, for SharePoint 2007 solutions I do it anyways out of habit and consistency. If you prefer not to use the “!important,” feel free to remove it.
This article was originally published on End User SharePoint.
ICF Ironworks is always on the lookout for experienced professionals who believe in hard work, having fun, and great client service.
Have you managed to do any work on the 2010 version?
Posted by: Mark | 01/05/2011 at 08:01 AM
I find that I don't really need to wrap web parts in my own div since in 2010 each web part is defined by the table with class "s4-wpTopTable". 2007 was missing a defining CSS hook for web parts. If the reason you want to wrap your web parts is to get divs for rounded corners, please look at using the Curvy Corners jQuery plugin instead--it works well.
Posted by: Brandon Anderson | 01/05/2011 at 09:11 AM