Wednesday, May 2, 2012

Customize the UI of web parts in SharePoint 2010


Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories:SharePoint Designer; CSS; Javascript and jQuery; Web Parts; Site Manager/Power User; MOSS; WSS; 2010

This post describes how to customize a standard Web Part in SharePoint by using SharePoint Designer 2010. Depending of the look and feel or factors such as accessibility, performance or cross browser stability it could be a challenge to bend the web part UI exactly in the way you want. The markup of the web parts in SharePoint contains quite a deep nested table structure and the elements do not always have an id or class.
When you need to create an advanced design for the web parts, the alternatives is to manipulate the DOM with jQuery or create custom web part control wrappers with code. Besides this, CSS 3 comes with some interesting possibilities like box shadow and border radius that lets you create stuff like boxes with curved corners but it’s not an option here because we need to render the master page in IE8 mode to get all functionalities intact in SharePoint 2010.
2012-01-31-CustomizeUI-01.png
So what can you do quick and easily then? The web part headers and all the cells in header row can easily be customized with CSS just like the borders around. I have made four examples with different colors, you can download and use, at the bottom of this page.
Using only CSS and images for rounded corners is the easiest approach where you don’t need plugins like CSS3 Pie or other htc solutions, fallback methods for different browsers or care about slow rendering when rewriting the already complex and nestled DOM. But when you reach the border of what is possible to do with just CSS and images it’s time to bend the DOM with our friend jQuery or fire up Visual Studio for writing a control adapter. Take a look at this blog post from ‘All things SharePoint’ that describes how to use jQuery to adding classes and wrappers into the DOM so you can get a wrapper with rounded corners for the web parts, and take a look at Waldek Mastykars post about how to removing the web part tables with code.
Back to the basics, let’s see how you can customize the web part headers the easy way.
The following example, see the image above, applies background images for the corners and for the middle section. The left and right corners have dimensions of 7 x 33 pixels, and the middle image has dimensions of 14 x 33 pixels.

Let’s go

Download the images and put them into Style Library/BlogBranding/Images or change the path in the CSS below if you want to put the images in some other folder.
01/* WebPart headers */
02/* All tdS in the row */
03.ms-WPHeader > TD{
04background-image: url('/Style Library/BlogBranding/Images/WP-MidGreen.png');
05background-repeat:repeat-x;
06padding-left:1px; padding-right:1px; height:33px;
07border-bottom-style:none!important;border-bottom-color:inherit!important; border-bottom-width:0px!important;
08}
09/* Left cell */
10.ms-WPHeader td:first-child {
11width:5px;
12background-image:url('/Style Library/BlogBranding/Images/WP-LeftGreen.png')!important;
13background-repeat:no-repeat;
14}
15/* Right cell */
16.ms-wpTdSpace {
17width:7px;
18background-image:url('/Style Library/BlogBranding/Images/WP-RightGreen.png')!important;
19background-repeat:no-repeat;
20background-color:transparent;
21}
22/* Arrow */
23.ms-WPHeaderTdMenu{
24background-color:transparent;
25border:0px!important;
26}
27/* Web part title */
28.ms-WPTitle {
29padding-left:10px;
30font-family:Arial, Helvetica, sans-serif;
31color:#fff;
32font-weight:bold;
33margin-bottom:1px;
34font-size:14px;
35}
36/* linked title and visited */
37.ms-WPTitle a, .ms-WPTitle a:visited {
38color:#fff;
39text-decoration:none;
40}
41/* hover title */
42.ms-WPTitle a:hover {
43color:#333;
44text-decoration:none;
45}
46/* hover web part menu */
47.ms-WPHeaderTdMenu:hover{
48border-left:1px solid transparent;
49background-image: url('/Style Library/BlogBranding/Images/WP-MidGreen.png');
50}

More stuff you can do

Do you want to do something special just for one single web part based on some condition or similar? Let’s say you want to have a different look and feel for some web part only if its header text is ‘Links’. I made up an example that makes the header title red and adds a border with a dropshadow filter older IEs and BoxShadow for modern browsers. Other web parts will not apply this look and feel.
2012-01-31-CustomizeUI-02.jpg

Here’s the jQuery

01$(document).ready(function() {
02var WPtitle = $('.ms-WPTitle span');
03for (var i = 0; i <= WPtitle.length; i++) {
04    if ($(WPtitle[i]).text() == 'Links') {
05            $(WPtitle[i]).css({'color': 'red'});
06            $(WPtitle[i]).parents().eq(10).addClass("WebPartBorder");
07    }
08    else if ($(WPtitle[i]).text() == 'Shared Documents') {
09            $(WPtitle[i]).css({'color': 'blue'});
10    }
11}
12});

Here’s the CSS

01/* All TDs in the table row */
02.ms-WPHeader TD{
03background-color: #f7f7f7;
04filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ebebeb', endColorstr='#e2e2e2');
05background: -webkit-gradient(linear, left top, left bottom, from(#e2e2e2), to(#ebebeb));
06background: -moz-linear-gradient(top, #e2e2e2, #ebebeb);
07border-top:1px #cfcfcf solid; border-bottom:1px #e2e2e2 solid!important;
08padding:3px;
09}
10/* for css / jquery drop shadow */
11.WebPartBorder{
12border:1px #777 solid!important;
13-moz-box-shadow: 5px 5px 5px #ccc;
14-webkit-box-shadow: 5px 5px 5px #ccc;
15box-shadow: 5px 5px 5px #ccc;/* CSS3 */
16background-color:#fff;
17filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=5, OffY=5, Color=#e9e9e9);   
18}
19.WebPartBorder > tbody {
20background-color:#f7f7f7!important
21}
22/* Border to the sides */
23.ms-WPHeader td:first-child {
24border-left:1px #e2e2e2 solid;
25border-right:0px;
26}
27.ms-wpTdSpace {
28border-right:1px #e2e2e2 solid;
29}
30/* Web part title */
31.ms-WPTitle {
32color: #333;
33font-weight:bold;
34}
35/* linked title and visited */
36.ms-WPTitle a, .ms-WPTitle a:visited {
37color: #333!important;
38text-decoration:none!important;
39}
40/* hover title */
41.ms-WPTitle a:hover {
42color:#0072bc!important;
43}
44/* hover web part menu */
45.ms-WPHeaderTdMenu:hover{
46 background-image:none;
47border-top:1px solid #ccc;
48border-right:1px solid transparent;
49border-left:1px solid transparent;
50background-color:#f7f7f7;
51}
52/* for the jQuery */
53.LinkTextColor {
54color:red;
55}
Hopes this gives you some ideas, and please feel free to drop a comment!
Christian Ståhl

No comments:

Post a Comment