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.
The following is that solution, all packaged up, ready for you to plug-and-play.
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.
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:
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.
- 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.